Attached Behavior
并MVVM
齐头并进。
通过附加行为处理您的事件并提供Viewmodel.ICommand
给它,它会在处理事件时执行。然后,您可以将已处理事件中的事件参数发送到ViewModel.ICommand
as 命令参数。
您的附属财产
public static class MyBehaviors {
public static readonly DependencyProperty CellActivatedCommandProperty
= DependencyProperty.RegisterAttached(
"CellActivatedCommand",
typeof(ICommand),
typeof(MyBehaviors),
new PropertyMetadata(null, OnCellActivatedCommandChanged));
public static ICommand CellActivatedCommand(DependencyObject o)
{
return (ICommand)o.GetValue(CellActivatedCommandProperty);
}
public static void SetCellActivatedCommand(
DependencyObject o, ICommand value)
{
o.SetValue(CellActivatedCommandProperty, value);
}
private static void OnCellActivatedCommandChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var xamDataGrid = d as XamDataGrid;
var command = e.NewValue as ICommand;
if (xamDataGrid != null && command != null)
{
xamDataGrid.CellActivated +=
(o, args) =>
{
command.Execute(args);
};
}
}
}
你的 XAML:
<infragistics:XamDataGrid ...
local:MyBehaviors.CellActivatedCommand="{Binding MyViewModelCommand}" />
希望能帮助到你。