如果单击行并将其列从 true 更改为 false,我如何在 DataGrid 中进行检测,反之亦然?
How can I detect in a DataGrid if row is clicked and change its column from true to false and vice versa?
我正在开发一个小型 WPF 应用程序,当我单击一行时,我正在创建我的复选框列 selected/unselected。这是我的行的样子:
这是我的代码:
private void dtgTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (dtgTest.SelectedItem != null)
{
CheckBox checkbocColumn = (dtgTest.Columns[3].GetCellContent(dtgTest.SelectedItem) as CheckBox);
checkbocColumn.IsChecked = !checkbocColumn.IsChecked;
var selectedItem = (BillItemInSerie)dtgTest.SelectedItem;
var obj = serialNumbersIn.FirstOrDefault(sn => selectedItem.DocumentItemInSeriesId == sn.DocumentItemInSeriesId);
obj.IsChecked= (bool)checkbocColumn.IsChecked;
}
}
这是我填充 DataGrid 的方式:
public Test_Window()
: this()
{
databaseValues = Controller.Instance.GetById(Id);
dtgTest.ItemsSource = null;
dtgTest.ItemsSource = databaseValues;
}
因此,当生成表单时,我实际上从数据库中获取了所有项目
这是我的 XAML:
<DataGrid Name="dtgTest" IsReadOnly="True" VirtualizingStackPanel.VirtualizationMode="Standard" EnableColumnVirtualization = "True" EnableRowVirtualization ="True" MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="30" Grid.ColumnSpan="2" Grid.Row="2" SelectionChanged="dtgTest_SelectionChanged">
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#0091EA"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Value1}" Header="" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*" />
<DataGridTextColumn Binding="{Binding Value2}" Header="" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*" />
<DataGridTextColumn x:Name="colFormatedDate" Binding="{Binding ExpireDate, StringFormat ={}{0:MM/yyyy}}" Header="" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="20*" />
<DataGridCheckBoxColumn x:Name="colSelektiraj" Binding="{Binding IsChecked}" Header="" Width="10*" />
</DataGrid.Columns>
</DataGrid>
但是,当我单击一行并选中复选框列时,问题就出现了(如上面的示例图像),如果我立即改变主意并再次单击选中的行来更改复选框列的状态我将无法执行此操作,因为 dtgTest_SelectionChanged
不会触发,因为我没有更改选择..
所以我想检测一行是否被点击可能对我有帮助?所以我可能会执行与 dtgTest_SelectionChanged
事件中类似的代码?
任何形式的帮助都会很棒!
谢谢大家
干杯
在 Rekshino 帮助后编辑:
<DataGrid Name="dtgTest" IsReadOnly="True" VirtualizingStackPanel.VirtualizationMode="Standard" EnableColumnVirtualization = "True" EnableRowVirtualization ="True" MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="30" Grid.ColumnSpan="2" Grid.Row="2" SelectionChanged="dtgTest_SelectionChanged" PreviewMouseDown="dtgTest_PreviewMouseDown">
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#0091EA"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="dtgTest_PreviewMouseDown"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Value1}" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*" />
<DataGridTextColumn Binding="{Binding Value2}" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*" />
<DataGridTextColumn x:Name="colFormatedDate" Binding="{Binding ExpireDate, StringFormat ={}{0:MM/yyyy}}" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="20*" />
<DataGridCheckBoxColumn x:Name="colSelektiraj" Binding="{Binding IsChecked, NotifyOnTargetUpdated=True}" Header="" Width="10*" />
</DataGrid.Columns>
</DataGrid>
C# :
private void dtgTest_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var cell = sender as DataGridCell;
if (cell == null)
{
return;
}
DataGridRow parGridRow = null;
var visParent = VisualTreeHelper.GetParent(cell);
while (parGridRow == null && visParent != null)
{
parGridRow = visParent as DataGridRow;
visParent = VisualTreeHelper.GetParent(visParent);
}
if (parGridRow == null) { return; }
var selectedItem = (parGridRow.DataContext as BillItemInSerie);
var obj = serialNumbersIn.FirstOrDefault(sn => selectedItem.DocumentItemInSeriesId == sn.DocumentItemInSeriesId);
obj.IsChecked = (bool)!obj.IsChecked;
}
如果您有一个 ViewModel (MVVM),它表示每一行的数据。你 should/could 有一个 bool 属性 代表 CheckBox 状态(而不是从代码后面这样做)。
接下来,您可以将 DataGrid 的 SelectedItem 绑定到一个对象(相关类型)并测试它是否会在您多次单击同一项目时被触发。如果是这样(至少在 DevExpress gridControls 中) - 您应该在 Setter 中为 SelectedItem 翻转 bool 属性。
您好,您可以在复选框列的绑定上设置一个标志。
当目标(您的复选框)更改时,有一个 NotifyOnTargetUpdated
会触发 TargetUpdated
事件。
在绑定上使用 NotifyOnTargetUpdated=True
即可激活它们。
你的 DataGridCheckBoxColumn
看起来像这样:
<DataGridCheckBoxColumn x:Name="colSelektiraj" Binding="{Binding IsChecked, NotifyOnTargetUpdated=True}" Header="" Width="10*" />
在 Datagrid 上使用 TargetUpdated="DataGrid_TargetUpdated"
事件来获得通知。你的数据网格看起来像这样:
<DataGrid Name="dtgTest" IsReadOnly="True" TargetUpdated="DataGrid_TargetUpdated" ...>
要在选择更改时收到通知,请像您已经使用的那样使用 SelectionChanged
事件。
您可以为单元格设置鼠标事件处理程序,获取行并根据需要进行操作。我已经删除了 SelectionChanged
的事件处理程序,因为您在此解决方案中不需要它。
<DataGrid Name="dtgTest" IsReadOnly="True" VirtualizingStackPanel.VirtualizationMode="Standard" EnableColumnVirtualization = "True" EnableRowVirtualization ="True" MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="30" Grid.ColumnSpan="2" Grid.Row="2">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<!-- If you have to apply another style, then use BasedOn-->
<!--<Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridCentering}">-->
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="PreviewMouseDown"/>
</Style>
</DataGrid.Resources>
...
</DataGrid>
private void PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var cell = sender as DataGridCell; if (cell == null) { return; }
DataGridRow parGridRow = null;
var visParent = VisualTreeHelper.GetParent(cell);
while (parGridRow == null && visParent != null)
{
parGridRow = visParent as DataGridRow;
visParent = VisualTreeHelper.GetParent(visParent);
}
if (parGridRow == null) { return; }
var selectedItem = (parGridRow.DataContext as BillItemInSerie);
var obj = serialNumbersIn.FirstOrDefault(sn => selectedItem.DocumentItemInSeriesId == sn.DocumentItemInSeriesId);
obj.IsChecked= (bool)!obj.IsChecked;
//e.Handled = true;
}
我正在开发一个小型 WPF 应用程序,当我单击一行时,我正在创建我的复选框列 selected/unselected。这是我的行的样子:
这是我的代码:
private void dtgTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (dtgTest.SelectedItem != null)
{
CheckBox checkbocColumn = (dtgTest.Columns[3].GetCellContent(dtgTest.SelectedItem) as CheckBox);
checkbocColumn.IsChecked = !checkbocColumn.IsChecked;
var selectedItem = (BillItemInSerie)dtgTest.SelectedItem;
var obj = serialNumbersIn.FirstOrDefault(sn => selectedItem.DocumentItemInSeriesId == sn.DocumentItemInSeriesId);
obj.IsChecked= (bool)checkbocColumn.IsChecked;
}
}
这是我填充 DataGrid 的方式:
public Test_Window()
: this()
{
databaseValues = Controller.Instance.GetById(Id);
dtgTest.ItemsSource = null;
dtgTest.ItemsSource = databaseValues;
}
因此,当生成表单时,我实际上从数据库中获取了所有项目
这是我的 XAML:
<DataGrid Name="dtgTest" IsReadOnly="True" VirtualizingStackPanel.VirtualizationMode="Standard" EnableColumnVirtualization = "True" EnableRowVirtualization ="True" MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="30" Grid.ColumnSpan="2" Grid.Row="2" SelectionChanged="dtgTest_SelectionChanged">
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#0091EA"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Value1}" Header="" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*" />
<DataGridTextColumn Binding="{Binding Value2}" Header="" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*" />
<DataGridTextColumn x:Name="colFormatedDate" Binding="{Binding ExpireDate, StringFormat ={}{0:MM/yyyy}}" Header="" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="20*" />
<DataGridCheckBoxColumn x:Name="colSelektiraj" Binding="{Binding IsChecked}" Header="" Width="10*" />
</DataGrid.Columns>
</DataGrid>
但是,当我单击一行并选中复选框列时,问题就出现了(如上面的示例图像),如果我立即改变主意并再次单击选中的行来更改复选框列的状态我将无法执行此操作,因为 dtgTest_SelectionChanged
不会触发,因为我没有更改选择..
所以我想检测一行是否被点击可能对我有帮助?所以我可能会执行与 dtgTest_SelectionChanged
事件中类似的代码?
任何形式的帮助都会很棒! 谢谢大家
干杯
在 Rekshino 帮助后编辑:
<DataGrid Name="dtgTest" IsReadOnly="True" VirtualizingStackPanel.VirtualizationMode="Standard" EnableColumnVirtualization = "True" EnableRowVirtualization ="True" MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="30" Grid.ColumnSpan="2" Grid.Row="2" SelectionChanged="dtgTest_SelectionChanged" PreviewMouseDown="dtgTest_PreviewMouseDown">
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#0091EA"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="dtgTest_PreviewMouseDown"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Value1}" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*" />
<DataGridTextColumn Binding="{Binding Value2}" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*" />
<DataGridTextColumn x:Name="colFormatedDate" Binding="{Binding ExpireDate, StringFormat ={}{0:MM/yyyy}}" Foreground="Black" FontSize="15" FontFamily="Verdana" Width="20*" />
<DataGridCheckBoxColumn x:Name="colSelektiraj" Binding="{Binding IsChecked, NotifyOnTargetUpdated=True}" Header="" Width="10*" />
</DataGrid.Columns>
</DataGrid>
C# :
private void dtgTest_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var cell = sender as DataGridCell;
if (cell == null)
{
return;
}
DataGridRow parGridRow = null;
var visParent = VisualTreeHelper.GetParent(cell);
while (parGridRow == null && visParent != null)
{
parGridRow = visParent as DataGridRow;
visParent = VisualTreeHelper.GetParent(visParent);
}
if (parGridRow == null) { return; }
var selectedItem = (parGridRow.DataContext as BillItemInSerie);
var obj = serialNumbersIn.FirstOrDefault(sn => selectedItem.DocumentItemInSeriesId == sn.DocumentItemInSeriesId);
obj.IsChecked = (bool)!obj.IsChecked;
}
如果您有一个 ViewModel (MVVM),它表示每一行的数据。你 should/could 有一个 bool 属性 代表 CheckBox 状态(而不是从代码后面这样做)。
接下来,您可以将 DataGrid 的 SelectedItem 绑定到一个对象(相关类型)并测试它是否会在您多次单击同一项目时被触发。如果是这样(至少在 DevExpress gridControls 中) - 您应该在 Setter 中为 SelectedItem 翻转 bool 属性。
您好,您可以在复选框列的绑定上设置一个标志。
当目标(您的复选框)更改时,有一个 NotifyOnTargetUpdated
会触发 TargetUpdated
事件。
在绑定上使用 NotifyOnTargetUpdated=True
即可激活它们。
你的 DataGridCheckBoxColumn
看起来像这样:
<DataGridCheckBoxColumn x:Name="colSelektiraj" Binding="{Binding IsChecked, NotifyOnTargetUpdated=True}" Header="" Width="10*" />
在 Datagrid 上使用 TargetUpdated="DataGrid_TargetUpdated"
事件来获得通知。你的数据网格看起来像这样:
<DataGrid Name="dtgTest" IsReadOnly="True" TargetUpdated="DataGrid_TargetUpdated" ...>
要在选择更改时收到通知,请像您已经使用的那样使用 SelectionChanged
事件。
您可以为单元格设置鼠标事件处理程序,获取行并根据需要进行操作。我已经删除了 SelectionChanged
的事件处理程序,因为您在此解决方案中不需要它。
<DataGrid Name="dtgTest" IsReadOnly="True" VirtualizingStackPanel.VirtualizationMode="Standard" EnableColumnVirtualization = "True" EnableRowVirtualization ="True" MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="30" Grid.ColumnSpan="2" Grid.Row="2">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<!-- If you have to apply another style, then use BasedOn-->
<!--<Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridCentering}">-->
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="PreviewMouseDown"/>
</Style>
</DataGrid.Resources>
...
</DataGrid>
private void PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var cell = sender as DataGridCell; if (cell == null) { return; }
DataGridRow parGridRow = null;
var visParent = VisualTreeHelper.GetParent(cell);
while (parGridRow == null && visParent != null)
{
parGridRow = visParent as DataGridRow;
visParent = VisualTreeHelper.GetParent(visParent);
}
if (parGridRow == null) { return; }
var selectedItem = (parGridRow.DataContext as BillItemInSerie);
var obj = serialNumbersIn.FirstOrDefault(sn => selectedItem.DocumentItemInSeriesId == sn.DocumentItemInSeriesId);
obj.IsChecked= (bool)!obj.IsChecked;
//e.Handled = true;
}