【发布时间】:2017-09-13 19:48:24
【问题描述】:
我正在尝试找到一种在代码中添加行为的方法,我能够在 XAML 中成功添加它。
这就是我将 XAML 中的行为添加到网格的方式,SelectedItems 是行为中的 DP,它是数据绑定到视图模型选定项属性。我正在监听网格集合更改事件并更新 DP,从而通知所选项目的视图模式
/// <summary>
/// Dependency Property SelectedItems
/// </summary>
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems",
typeof(INotifyCollectionChanged), typeof(MultiSelectBehavior),
new PropertyMetadata(null));
AssociatedObject.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged;
<i:Interaction.Behaviors>
<behaviors:MultiSelectBehavior SelectedItems="{Binding SelectedItems}"/>
</i:Interaction.Behaviors>
我需要在后面的代码中创建这种行为。我在包含网格的窗口的构造函数中执行此操作,但它不起作用,viewmodel selected items 属性没有得到更新。
var multiSelectBehavior = new MultiSelectBehaviorSingleton();
BindingOperations.SetBinding(this.BackupsGrid, MultiSelectBehavior.SelectedItemsProperty,
new Binding()
{
Source = this.DataContext,
Path = new PropertyPath("SelectedItems"),
Mode = BindingMode.OneWay
});
Interaction.GetBehaviors(this.BackupsGrid).Add(multiSelectBehavior);
【问题讨论】:
标签: wpf wpf-controls