我正在使用WPF LOB應用程序並使用Prism和委託命令將UI與View Model分開。如何在WPF MVVM應用程序中傳遞DataGridCellEditEventArgs
當用戶對特定的單元格從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}"
當被調用時,我沒有得到任何的EventArgs中調用相同我的代理命令在虛擬機,我如何檢索事件參數,我可以通過命令參數設置它?如果是這樣,我如何將事件參數傳遞給委託命令?
在CellEditEndigEvent期間,該值尚未存儲到VM中,因爲它仍處於轉換中,是否有辦法強制它從處理程序發生,所以我不需要從中讀取值CellEditEndingEventArgs,而是我可以直接從VM讀取?