我有一个带DataGrid的WPF应用程序,我希望用户能够双击一行以打开一个编辑对话框。
所以我在ViewModel和InputBinding中添加了SelectedItem属性到网格。现在,我的ViewModel中有一个命令,当用户双击网格时会触发该命令。我也得到了正确的选择项。到目前为止,一切都很好。
问题是,当用户单击网格上的某个空白空间时,也会触发该事件(我在图片上标记了它)。
用户应该不能对空白区域执行双击操作。因为事件触发时SelectedItem保持不变。所以这是错误的。
我的DataGrid XAML代码:
<DataGrid Name="dgSafetyFunction" AutoGenerateColumns="False" ItemsSource="{Binding SafetyFunctionList}" Margin="0,0,0,45"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" SelectionUnit="FullRow">
<DataGrid.InputBindings>
<MouseBinding
MouseAction="LeftDoubleClick"
Command="{Binding OnDataGridDoubleClickCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Beschreibung" Binding="{Binding Description}"/>
<DataGridTextColumn Header="Projekt ID" Binding="{Binding ProjectID}"/>
</DataGrid.Columns>
</DataGrid>
SelectedItem属性:
private SafetyFunctionModel m_SelectedItem;
public SafetyFunctionModel SelectedItem
{
get
{
return m_SelectedItem;
}
set
{
if (value != m_SelectedItem)
{
m_SelectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
}
如何解决MVVM问题?
问候
最佳答案
定义一个CellTemplate
并将MouseBinding
移到该代码的根元素中:
<DataGrid Name="dgSafetyFunction" AutoGenerateColumns="False" ItemsSource="{Binding SafetyFunctionList}" Margin="0,0,0,45"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Beschreibung" Binding="{Binding Description}"/>
<DataGridTextColumn Header="Projekt ID" Binding="{Binding ProjectID}"/>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<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}"/>
<Border.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding DataContext.OnDataGridDoubleClickCommand,
RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</Border.InputBindings>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
关于c# - C#WPF MVVM DataGrid MouseBinding仅适用于行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58423744/