3

我在 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>

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

我的 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.

任何帮助深表感谢。

4

1 回答 1

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;
   }
};
于 2014-02-20T18:45:28.143 回答