Silverlight:父视图模型属性值到子视图模型属性



我在 Silverlight 论坛上发布了这个问题,但一直无法得到解决我问题的答案,所以我希望 SO 的大师可以提供帮助!

基本上,我的父视图模型中有一个实体属性。 当此实体更改时,我需要子视图模型中实体的 ID。 我创建了一个具有依赖项属性的子控件,并在构造函数中创建了一个绑定。 我正在尝试使用 MVVM 和 MEF 实现所有这些。

我的家长观模型:

[ExportPlugin(ViewModelTypes.ParentViewModel, PluginType.ViewModel)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ParentViewModel: ViewModelBase
{
    private Person _currentPerson;
    public Person CurrentPerson
    {
        get { return _currentPerson; }
        private set
        {
            if (!ReferenceEquals(_currentPerson, value))
            {
                _currentPerson= value;
                RaisePropertyChanged("CurrentPerson");
            }
        }
    }
}

我的父用户控件:

<UserControl x:Class="MyApp.ParentUserControl" x:Name="ParentControl">
        <local:ChildUserControl PersonID="{Binding ElementName=ParentControl, Mode=TwoWay, Path=DataContext.CurrentPerson.ID}" />
</UserControl>

我的儿童用户控件代码隐藏:

public partial class ChildUserControl : UserControl
{
    #region Private Properties
    private PluginCatalogService _catalogService = PluginCatalogService.Instance;
    #endregion
    #region Dependency Properties
    public static readonly DependencyProperty PersonIDProperty = 
        DependencyProperty.Register("PersonID", typeof(int), typeof(ChildUserControl), new PropertyMetadata(OnPersonIDChanged));
    #endregion
    #region Public Properties
    public int PersonID
    {
        get { return (int)GetValue(PersonIDProperty); }
        set { SetValue(PersonIDProperty, value); }
    }
    #endregion
    #region Constructor
    public ChildUserControl()
    {
        InitializeComponent();
        if (!ViewModelBase.IsInDesignModeStatic)
            this.DataContext = _catalogService.FindPlugin(ViewModelTypes.ChildViewModel, PluginType.ViewModel);
        this.SetBinding(PersonIDProperty, new Binding("PersonID") { Mode = BindingMode.TwoWay, Source = DataContext, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
    }
    #endregion
private static void OnPersonIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ...
}

我的孩子视图模型:

[ExportPlugin(ViewModelTypes.ChildViewModel, PluginType.ViewModel)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChildViewModel: ViewModelBase
{
    private int _personID;
    public int PersonID
    {
        get { return _personID; }
        set
        {
            if (!ReferenceEquals(_personID, value))
            {
                _personID= value;
                RaisePropertyChanged("PersonID");
            }
        }
    }
}

我创建了 OnPersonIDChanged 事件,以查看当CurrentPerson实体更改时,是否在 ChildControl 中选取了更改,确实如此。 它只是没有在ChildControl ViewModel中被捡到.

任何帮助都非常感谢。

最好,如果您使用的是PRISM,则可以使用EventAggregator...请参阅 http://msdn.microsoft.com/en-US/library/FF921122(v=pandp.40).aspx 和 https://compositewpf.codeplex.com/

另一种选择是将(hack)挂接到PropertyChanged上

ViewModel1.PropertyChanged += (s, e) =>
{
   if (e.PropertyName == "XXX")
   {
      ViewModel2.PropertyX = vm1.PropertY;
   }
};

最新更新