2013-03-26 189 views
1

我有以下情况:WPF MVVM依赖属性

  1. 我那里有内部单个网格用户控件。
  2. 网格具有其第一列作为复选框列,这势必CustomerModel的IsSelected属性
  3. 的ItemsSource为网格被绑定到列表< CustomerModel>
  4. 当用户检查任何的复选框的相应CustomerModel的IsSelected属性是得到更新

查询:

  1. 我添加了一个依赖属性的UserCon特伦名为“SelectedCustomerItems”,我希望它返回一个列表< CustomerModel>(仅用于IsSelected =真)

  2. 该用户控件放置在另一个窗口

  3. 依赖属性“SelectedCustomerItems”势必“ SelectedCustomers“属性WindowViewModel

但我没有通过此依赖项属性获取SelectedCustomer项目。断点不获取命中{}请建议....

+0

只是包装的'CustomerModel'在'CustomerViewModel'其中包含了'IsSelected'财产。您不需要在'UI'中放置'DependencyProperties'来保存数据。 – 2013-03-26 20:45:24

+0

感谢HiCore,我已经拥有了它。我面临的问题是:当我给这个用户控制给另一个用户时,他如何从UserControl中将SelectedCustmerItems放入他的ViewModel – user1386121 2013-03-26 21:00:52

+1

“Selected/Non Selected”不是View(IMO)的责任。相反,有一个“CheckBox”绑定到事物的IsSelected属性。那么它就是'.Where(x => X.IsSelected)'的问题。 – 2013-03-26 21:07:02

回答

6

这种方式实现你的DPS:

#region SomeProperty 
/// <summary> 
/// The <see cref="DependencyProperty"/> for <see cref="SomeProperty"/>. 
/// </summary> 
public static readonly DependencyProperty SomePropertyProperty = 
    DependencyProperty.Register(
     SomePropertyPropertyName, 
     typeof(object), 
     typeof(SomeType), 
     // other types here may be appropriate for your situ 
     new FrameworkPropertyMetadata(null, OnSomePropertyPropertyChanged)); 

/// <summary> 
/// Called when the value of <see cref="SomePropertyProperty"/> changes on a given instance of <see cref="SomeType"/>. 
/// </summary> 
/// <param name="d">The instance on which the property changed.</param> 
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> 
private static void OnSomePropertyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    (d as SomeType).OnSomePropertyChanged(e.OldValue as object, e.NewValue as object); 
} 

/// <summary> 
/// Called when <see cref="SomeProperty"/> changes. 
/// </summary> 
/// <param name="oldValue">The old value</param> 
/// <param name="newValue">The new value</param> 
private void OnSomePropertyChanged(object oldValue, object newValue) 
{ 

} 

/// <summary> 
/// The name of the <see cref="SomeProperty"/> <see cref="DependencyProperty"/>. 
/// </summary> 
public const string SomePropertyPropertyName = "SomeProperty"; 

/// <summary> 
/// 
/// </summary> 
public object SomeProperty 
{ 
    get { return (object)GetValue(SomePropertyProperty); } 
    set { SetValue(SomePropertyProperty, value); } 
} 
#endregion 

你必须明白,一个DependencyProperty不仅仅是财产与一群垃圾增加的,其钩入WPF绑定系统。这是一个庞大而复杂的系统,它生活在海平面以下,你的DP难以漂浮。它的行为方式是你不会期待的,除非你真的了解它。

您正在经历我们所有人对DP的第一次启示:绑定不会通过属性访问器访问DependencyProperty值(即,getset方法)。这些属性访问器是便于您使用的代码只有。你可以放弃它们并使用DependencyObject.GetValue and DependencyObject.SetValue,它们是系统中的实际钩子(参见上面我的示例中的获取器/设置器的实现)。

如果你想听更改通知,你应该做我在上面做的例子。注册DependencyProperty时,您可以添加更改通知侦听器。您也可以覆盖继承的DependencyProperties的“元数据”(为DataContext我总是这样做)以添加更改通知(有些使用DependencyPropertyDescriptor,但我发现它们缺少)。

但是,无论您做什么,都不要将代码添加到您的DependencyProperties的getset方法!它们不会被绑定操作执行。

有关DependencyProperties如何工作的更多信息,I highly suggest reading this great overview on MSDN.