What i would like to achieve is when user changes a weekrange
on View1
, id like the dropdown value on View2
to update as well. As it stands, One does not update the other.
My setup
Bootstrapper.cs
Using prism
, I've created a singleton type that I would need to share between 2 views.
class Bootstrapper : UnityBootstrapper
{
protected override void ConfigureContainer()
{
base.ConfigureContainer();
...
Container.RegisterType<IRTGSingletonService, RTGSingletonService>(new ContainerControlledLifetimeManager());
...
}
}
IRTGSingletonService.cs / RTGSingletonService.cs
This object has 2 Enums SortBy
and Weekrange
and 2 EnumDataSource
. lets focus on Weekrange
public interface IRTGSingletonService
{
WeekRanges WeekRange { get; set; }
EnumDataSource WeekRangeDataSource { get; set; }
}
public class RTGSingletonService : BindableBase, IRTGSingletonService
{
private readonly IEventAggregator _ea;
private readonly IRepository _repository;
private WeekRanges _weekRanges;
public RTGSingletonService(IEventAggregator ea, IRepository repo)
{
_ea = ea;
_repository = repo;
WeekRange = WeekRanges.LastMonth;
}
#region Week Range
public EnumDataSource WeekRangeDataSource { get; set; } = new EnumDataSource { EnumType = typeof(WeekRanges) };
public WeekRanges WeekRange
{
get => _weekRanges;
set => SetProperty(ref _weekRanges, value, OnWeekRangeChanged);
}
private void OnWeekRangeChanged()
{
// TODO
}
#endregion
}
WeekRangesControl.xaml
To prevent repeating myself, Ive created a reusable control.
<UserControl x:Class="AdImpactAnalysis.Controls.WeekRangesControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot">
<telerik:RadComboBox ToolTip="Select a date range."
ItemsSource="{Binding DataSource}"
SelectedIndex="{Binding SelectedItem, Mode=TwoWay, Converter={StaticResource EnumConverter}}"
SelectedValuePath="Name" />
</Grid>
</UserControl>
WeekRangesControl.cs
public partial class WeekRangesControl : UserControl
{
/// <summary> Identified the DataSource dependency property </summary>
public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register(nameof(DataSource), typeof(EnumDataSource), typeof(WeekRangesControl));
/// <summary> Identified the SelectedItem dependency property </summary>
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(nameof(SelectedItem), typeof(WeekRanges), typeof(WeekRangesControl));
/// <summary> Gets or sets the DataSource. </summary>
public EnumDataSource DataSource
{
get => (EnumDataSource)GetValue(DataSourceProperty);
set => SetValue(DataSourceProperty, value);
}
/// <summary> Gets or sets the SelectedItem. </summary>
public WeekRanges SelectedItem
{
get => (WeekRanges)GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
public WeekRangesControl()
{
InitializeComponent();
LayoutRoot.DataContext = this;
}
}
View1.xaml / View2.xaml
These 2 views are tabs, only one view can be visible to the user at a time. (Kinda hoping this wont be an issue...)
This control is added to my 2 views like below
<StackPanel Grid.Column="0">
<controls:WeekRangesControl DataSource="{Binding SingletonService.WeekRangeDataSource}"
SelectedItem="{Binding SingletonService.WeekRange, Mode=TwoWay, Converter={StaticResource EnumConverter}}" />
</StackPanel>
View1ViewModel.cs / View2ViewModel.cs
public View1ViewModel(IEventAggregator ea, IRepository repo, IRTGGroupsService gm, IRTGPredictionsService<RTG_Lookup2_Group> pm, IRTGSingletonService ss)
{
...
}
public View2ViewModel(IEventAggregator ea, IRepository repo, IRTGGroupsService gm, IRTGPredictionsService<RTG_Lookup2_Group> pm, IRTGSingletonService ss)
{
...
}
The issue I am having is when i select a weekrange
on View1
, View2
shows a different selected value.
Something i noticed and worth mentioning is, from here, the first time View2
is changed, my weekrange
setter does not fire until i select a weekrange a second time. But even then, View1
will not update with the value from View2
.
Any help would be appreciated. thanks