如果单击行外部,如何删除Row的选择。就像在下面显示的图像的红色区域
相关的xaml如下所示:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<DataGrid x:Name="ClientsList" Grid.Row="0" Grid.Column="0" CanUserAddRows="False"
SelectionMode="Extended" SelectionUnit="FullRow"
ItemsSource="{Binding ClientItems}" LostFocus="ClientsList_LostFocus" Background="Green">
</DataGrid>
<StackPanel Margin="2" Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<Button x:Name="AddButton" Margin="2,0,0,0" Content="+" Height="25" Width="40" Click="AddButton_Click"/>
<Button x:Name="DeleteButton" Margin="2,0,0,0" Content="-" Height="25" Width="40" Click="DeleteButton_Click"/>
</StackPanel>
</Grid>
答案 0 :(得分:5)
在您的情况下,如果单击空白区域,则OriginalSource
将为ScrollViewer
(如果未应用其他样式)。尝试将以下MouseUp
事件处理程序添加到DataGrid
:
private void OnDataGridMouseUp(object o, MouseButtonEventArgs e)
{
if (e.OriginalSource is ScrollViewer)
{
((DataGrid) o).UnselectAll();
}
}
答案 1 :(得分:0)