Silverlight:父ViewModel属性值为Child ViewModel属性

时间:2012-12-05 11:03:22

标签: silverlight-5.0

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

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

我的ParentViewModel:

[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");
            }
        }
    }
}

我的ParentUserControl:

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

我的ChildUserControl代码隐藏:

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

My ChildViewModel:

[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中被选中。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

最好,如果您使用PRISM,您可以使用EventAggregator ... 请参阅http://msdn.microsoft.com/en-us/library/ff921122(v=pandp.40).aspxhttps://compositewpf.codeplex.com/

另一个选择是挂钩(hack)到PropertyChanged

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