[英]WPF MVVM handle datagrid cell change
我试图赶上CellEditEnding事件并获取行+列号和新值到我的视图模型。
我尝试使用MVVM如何处理数据网格单元更改? 但我得到这个例外
“找不到类型'GalaSoft_MvvmLight_Command'。请验证您没有缺少程序集引用,并且所有引用程序集均已构建”。 我已经引用了GalaSoft.MvvmLight.WPF4.dll。
这是我的dataGrid:
<DataGrid MaxHeight="600" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding BitTestParam,Mode=TwoWay}" GridLinesVisibility="None" RowBackground="{x:Null}" Background="{x:Null}"
BorderThickness="2" BorderBrush="Black" HeadersVisibility="Column" Foreground="Black" FontStyle="Italic" FontWeight="SemiBold">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnding">
<i:InvokeCommandAction Command="{Binding CellChangedCommand}" CommandParameter="{Binding}"/>
<!--<GalaSoft_MvvmLight_Command:EventToCommand PassEventArgsToCommand="True" Command="{Binding CellEditEndingCommand}"/>-->
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="Input Param" Binding="{Binding InputNames}"/>
<DataGridTextColumn x:Name="InputValuesColumn" Header="Param value" Binding="{Binding InputValues}" />
<DataGridTextColumn Header="Output Param" Binding="{Binding OutputNames}"/>
<DataGridTextColumn Header="Measure value" Binding="{Binding OutputValues}"/>
<DataGridTextColumn Header="Error Messages" Binding="{Binding ErrorMessages}"/>
<DataGridTextColumn Header="Warning Messages" Binding="{Binding WarningMessages}" />
</DataGrid.Columns>
</DataGrid>
这是我在viewModel中的相关代码:
public RelayCommand<object> CellChangedCommand
{
get;
set;
}
CellChangedCommand = new RelayCommand<object>(CellChangedEvent);
void CellChangedEvent(object obj)
{
}
我得到带有参数的命令“ CellChangedCommand”,但我需要获取行+列号和新值。
谢谢
我将让用户在单独的面板中进行编辑,并在那里进行处理程序验证。 该示例就是这样工作的: https : //gallery.technet.microsoft.com/scriptcenter/WPF-Entity-Framework-MVVM-78cdc204
在那您将找到一些代码,这些代码可以算出哪个绑定只是将数据从视图传输到视图模型。 即使用户在数据网格中进行编辑,您仍然可以使用它。 您只需要在数据网格周围放置一个网格或面板即可。 它甚至可以在数据网格中工作,我必须尝试确定一下。 但是,如果您查看该项目的资源字典,将会看到:
<Grid Visibility="{Binding IsInEditMode, Converter={StaticResource BooleanToVisibilityConverter}}"
Width="{Binding ElementName=dg, Path=ActualWidth}"
Height="{Binding ElementName=dg, Path=ActualHeight}"
>
<i:Interaction.Triggers>
<local:RoutedEventTrigger RoutedEvent="{x:Static Validation.ErrorEvent}">
<e2c:EventToCommand
Command="{Binding EditVM.TheEntity.ConversionErrorCommand, Mode=OneWay}"
EventArgsConverter="{StaticResource BindingErrorEventArgsConverter}"
PassEventArgsToCommand="True" />
</local:RoutedEventTrigger>
<local:RoutedEventTrigger RoutedEvent="{x:Static Binding.SourceUpdatedEvent}">
<e2c:EventToCommand
Command="{Binding EditVM.TheEntity.SourceUpdatedCommand, Mode=OneWay}"
EventArgsConverter="{StaticResource BindingSourcePropertyConverter}"
PassEventArgsToCommand="True" />
</local:RoutedEventTrigger>
</i:Interaction.Triggers>
转换失败或删除失败由前者抓住,而数据传输则由后者抓住。
您需要标记到notifyonsourceupdated的绑定-一个示例绑定是:
<TextBox Text="{Binding EditVM.TheEntity.Address1,
UpdateSourceTrigger=PropertyChanged,
NotifyOnSourceUpdated=True,
NotifyOnValidationError=True,
Mode=TwoWay}" />
如果我错过了一些其他代码在示例中。 路由事件触发
public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
RoutedEvent routedEvent;
public RoutedEvent RoutedEvent
{
get
{
return routedEvent;
}
set
{
routedEvent = value;
}
}
public RoutedEventTrigger()
{
}
protected override void OnAttached()
{
Behavior behavior = base.AssociatedObject as Behavior;
FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
if (behavior != null)
{
associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
}
if (associatedElement == null)
{
throw new ArgumentException("This only works with framework elements");
}
if (RoutedEvent != null)
{
associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent));
}
}
void OnRoutedEvent(object sender, RoutedEventArgs args)
{
base.OnEvent(args);
}
protected override string GetEventName()
{
return RoutedEvent.Name;
}
}
绑定源属性转换器
public class BindingSourcePropertyConverter: IEventArgsConverter
{
public object Convert(object value, object parameter)
{
DataTransferEventArgs e = (DataTransferEventArgs)value;
BindingExpression binding = ((FrameworkElement)e.TargetObject).GetBindingExpression(e.Property);
return binding.ResolvedSourcePropertyName ?? "";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.