0
我試圖找到一種方法在代碼中添加行爲,我可以在XAML中成功添加它。如何在後面的代碼中添加WPF行爲
這是我如何將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);
@vikasmittal:請記得投了有用的答案:) https://stackoverflow.com/help/someone-answers – mm8