无需 Blend SDK 或第三方即可将事件绑定到命令。 (WPF/MVVM)
Binding Events to Commands without Blend SDK or third party. (WPF / MVVM)
有没有办法将事件绑定到命令 而无需 System.Windows.Interactivity
或任何其他第三方库?
到目前为止,我已经尝试了以下方法:
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick"
Handler="{Binding DoubleClickCommand}" />
</Style>
和
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}"/>
</DataGrid.InputBindings>
和
<DataGrid ItemsSource="{Binding Model.SpectrumCollections}"
ColumnWidth="*"
AutoGenerateColumns="false"
IsReadOnly="True"
BorderThickness="0"
HeadersVisibility="Column" MouseDoubleClick="{Binding DoubleClickCommand}">
但似乎没有任何效果,我们将不胜感激。
编辑 1:
这是我使用的RelayCommand的实现,也许问题出在这里..
public class RelayCommand : ICommand
{
private readonly Func<bool> _canExecute;
private readonly Action _execute;
public RelayCommand(Action inExecute, Func<bool> inCanExecute = null)
{
_execute = inExecute ?? throw new Helper.Exceptions.DelegateCommandException(
Exceptions.InExecuteIsNullException);
_canExecute = inCanExecute;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(object inObject) => _canExecute?.Invoke() ?? true;
public void Execute(object inObject) => _execute?.Invoke();
}
您不能执行以下操作,因为 MouseDoubleClick
是事件而不是依赖项 属性:
MouseDoubleClick="{Binding DoubleClickCommand}">
您可以绑定到 MouseBinding
的 Command
属性:
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}"/>
</DataGrid.InputBindings>
如果要在双击单元格时调用命令,应将MouseBinding
添加到DataGridCell
:
<DataGrid x:Name="dg" AutoGenerateColumns="False">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding DataContext.DoubleClickCommand,RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</ContentPresenter.InputBindings>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
有没有办法将事件绑定到命令 而无需 System.Windows.Interactivity
或任何其他第三方库?
到目前为止,我已经尝试了以下方法:
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick"
Handler="{Binding DoubleClickCommand}" />
</Style>
和
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}"/>
</DataGrid.InputBindings>
和
<DataGrid ItemsSource="{Binding Model.SpectrumCollections}"
ColumnWidth="*"
AutoGenerateColumns="false"
IsReadOnly="True"
BorderThickness="0"
HeadersVisibility="Column" MouseDoubleClick="{Binding DoubleClickCommand}">
但似乎没有任何效果,我们将不胜感激。
编辑 1: 这是我使用的RelayCommand的实现,也许问题出在这里..
public class RelayCommand : ICommand
{
private readonly Func<bool> _canExecute;
private readonly Action _execute;
public RelayCommand(Action inExecute, Func<bool> inCanExecute = null)
{
_execute = inExecute ?? throw new Helper.Exceptions.DelegateCommandException(
Exceptions.InExecuteIsNullException);
_canExecute = inCanExecute;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(object inObject) => _canExecute?.Invoke() ?? true;
public void Execute(object inObject) => _execute?.Invoke();
}
您不能执行以下操作,因为 MouseDoubleClick
是事件而不是依赖项 属性:
MouseDoubleClick="{Binding DoubleClickCommand}">
您可以绑定到 MouseBinding
的 Command
属性:
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}"/>
</DataGrid.InputBindings>
如果要在双击单元格时调用命令,应将MouseBinding
添加到DataGridCell
:
<DataGrid x:Name="dg" AutoGenerateColumns="False">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding DataContext.DoubleClickCommand,RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</ContentPresenter.InputBindings>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>