MVVM如何在2个视图之间共享属性

时间:2018-06-20 22:21:20

标签: c# mvvm prism prism-6

我想实现的是,当用户更改weekrange上的View1时,id也像View2上的下拉值一样要更新。就目前而言,一个不会更新另一个。

我的设置

Bootstrapper.cs

使用prism,我创建了一个单例类型,需要在2个视图之间共享。

class Bootstrapper : UnityBootstrapper
{
    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        ...
        Container.RegisterType<IRTGSingletonService, RTGSingletonService>(new ContainerControlledLifetimeManager());
        ...
    }
}

IRTGSingletonService.cs / RTGSingletonService.cs

此对象具有2个枚举SortByWeekrange和2个EnumDataSource。让我们专注于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

为了防止重复自己,我创建了可重复使用的控件。

<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

  

这2个视图是选项卡,用户一次只能看到一个视图。 (金田希望这不会成为问题...)

此控件已添加到我的以下2个视图中

    <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) 
    {
        ...
    }

我遇到的问题是,当我在weekrange上选择View1时,View2显示了一个不同的选定值。

我注意到并值得一提的是,从这里开始,View2第一次被更改,我的weekrange的二传手直到第二次选择星期几才触发。但是即使那样,View1也不会使用View2中的值进行更新。

任何帮助将不胜感激。谢谢

0 个答案:

没有答案