[英]WPF DataGrid double click via commands + INotifyPropertyChanged?
我希望在單擊DataGrid項時在我的ViewModel中執行一個命令。 作為參數,我希望有相應的行。
我在互聯網上找到了一種方法,但它使用了DependencyProperty
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/632ea875-a5b8-4d47-85b3-b30f28e0b827
我不在我的項目中使用DependencyProperty
,而是使用INotifyPropertyChanged
。 如何在不使用DependencyProperty
情況下實現“雙擊數據網格”commaind?
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
...
<DataGrid SelectedItem={Binding MySelectedItem, Mode=TwoWay}>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding YourCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
我通常使用AttachedCommandBehavior 。 它是3個類文件,可以添加到您的項目中,並允許您將命令附加到任何事件。
以下是如何使用它的示例:
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="local:CommandBehavior.Event" Value="MouseDoubleClick" />
<Setter Property="local:CommandBehavior.Action" Value="{Binding MyDoubleClickCommand}" />
<Setter Property="local:CommandBehavior.CommandParameter" Value="{Binding }" />
</Style>
MVVM Light Toolkit提供了EventToCommand行為,這應該能夠實現所需的行為(如果你不想使用框架,你總是可以自己動手)。
您可以使用此代碼隱藏代碼段來標識雙擊的行。
在注釋“//對基礎數據執行某些操作”的行中,您可以從Grid或行的DataContext獲取附加的ViewModel,並以行作為參數調用您的Command。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.