我正在开发 WPF LOB 应用程序并使用 Prism 和委托命令将 UI 与视图模型分开。
当用户从 UI(而不是视图模型或服务)对特定单元格进行更改时,我需要调用其他一些功能。
我已经创建了附加行为
public static class DataGridCellEditEndingBehaviour
{
private static readonly DependencyProperty CellEditEndingProperty
= DependencyProperty.RegisterAttached(
"CellEditEnding",
typeof(CellEditEnding),
typeof(DataGridCellEditEndingBehaviour),
null);
public static readonly DependencyProperty CommandProperty
= DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(DataGridCellEditEndingBehaviour),
new PropertyMetadata(OnSetCommandCallback));
public static readonly DependencyProperty CommandParameterProperty
= DependencyProperty.RegisterAttached(
"CommandParameter",
typeof(object),
typeof(DataGridCellEditEndingBehaviour),
new PropertyMetadata(OnSetCommandParameterCallback));
public static ICommand GetCommand(DataGrid control)
{
return control.GetValue(CommandProperty) as ICommand;
}
public static void SetCommand(DataGrid control, ICommand command)
{
control.SetValue(CommandProperty, command);
}
public static void SetCommandParameter(DataGrid control, object parameter)
{
control.SetValue(CommandParameterProperty, parameter);
}
public static object GetCommandParameter(DataGrid control)
{
return control.GetValue(CommandParameterProperty);
}
private static void OnSetCommandCallback
(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
DataGrid control = dependencyObject as DataGrid;
if (control != null)
{
CellEditEnding behavior = GetOrCreateBehavior(control);
behavior.Command = e.NewValue as ICommand;
}
}
private static void OnSetCommandParameterCallback
(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
DataGrid control = dependencyObject as DataGrid;
if (control != null)
{
CellEditEnding behavior = GetOrCreateBehavior(control);
behavior.CommandParameter = e.NewValue;
}
}
private static CellEditEnding GetOrCreateBehavior(DataGrid control)
{
CellEditEnding behavior =
control.GetValue(CellEditEndingProperty) as CellEditEnding;
if (behavior == null)
{
behavior = new CellEditEnding(control);
control.SetValue(CellEditEndingProperty, behavior);
}
return behavior;
}
}
public class CellEditEnding : CommandBehaviorBase<DataGrid>
{
public CellEditEnding(DataGrid control)
: base(control)
{
control.CellEditEnding += OnCellEditEnding;
}
private void OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
ExecuteCommand();
}
}
我可以使用同样的方法调用
local:DataGridCellEditEndingBehaviour.Command ="{Binding CellChangedCommand}"
当事件被调用时,我在VM的delegateCommand中没有得到任何事件参数,我如何检索事件参数,我可以通过命令参数设置它吗?如果是这样,我如何将事件参数传递给委托命令?
在 CellEditEndigEvent 期间,该值尚未存储到 VM 中,因为它仍在转换中,有没有办法可以强制它从处理程序发生,所以我不需要从 CellEditEndingEventArgs 读取值,而是可以直接从VM读取?