C# 如何在使用WPF选中datagrid中的复选框时获取行值?
若同一行中用户选中的'isacrier'和'oldLibelle'为空,那个么我将显示一个消息框,通知用户未经授权。如何在WPF中实现这一点 以下是我的wpf代码:C# 如何在使用WPF选中datagrid中的复选框时获取行值?,c#,wpf,checkbox,datagrid,C#,Wpf,Checkbox,Datagrid,若同一行中用户选中的'isacrier'和'oldLibelle'为空,那个么我将显示一个消息框,通知用户未经授权。如何在WPF中实现这一点 以下是我的wpf代码: <Window.Resources> <local:_Produits x:Key="_Produits"/> <CollectionViewSource x:Key="produitsViewSource" Source=
<Window.Resources>
<local:_Produits x:Key="_Produits"/>
<CollectionViewSource x:Key="produitsViewSource" Source="{Binding Produits, Source={StaticResource _Produits}}"/>
</Window.Resources>
<DataGrid x:Name="futureProductsDataGrid" Grid.Row="4" Grid.Column="0" Margin="20" ItemsSource="{Binding}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Code produit vinif" Binding="{Binding codeVinif}" Width="105" IsReadOnly="true"/>
<DataGridTextColumn Header="Libellé Actuel" Binding="{Binding oldLibelle}" Width="SizeToCells" IsReadOnly="true"/>
<DataGridTextColumn Header="Libellé Futur" Binding="{Binding newLibelle, Mode=TwoWay}" Width="SizeToCells"/>
<DataGridCheckBoxColumn Header="A créer ?" Binding="{Binding isACreer, Mode=TwoWay}" Width="80" >
<DataGridCheckBoxColumn.CellStyle>
<Style>
<EventSetter Event="CheckBox.Checked" Handler="OnChecked"/>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
以下是一个屏幕截图:
这将弹出一条消息bix,您可以按OK采取行动
MessageBoxResult result = MessageBox.Show("Please enter the empty field to proceed?", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Asterisk);
if (result == MessageBoxResult.OK)
{
}
请试一试。谢谢。试试这个:
private void OnChecked(object sender, RoutedEventArgs e)
{
CheckBox checkBox = (CheckBox)e.OriginalSource;
DataGridRow dataGridRow = VisualTreeHelpers.FindAncestor<DataGridRow>(checkBox);
var produit = dataGridRow.DataContext;
if (checkBox.IsChecked && String.IsNullOrEmpty(produit.oldLibelle)
{
// Show message box here...
}
e.Handled = true;
}
我不知道绑定到来自表适配器的网格的产品是什么样子的。
但是您应该能够在上面提到的produit对象中的某个地方找到oldLibelle属性
请注意,此解决方案使用Rachel Lim编写的自定义VisualTreeHelpers类。可以找到它。
我提供了上面使用的FindAcenstor方法
VisualTreeHelpers在内部使用.NET类。以下是完整代码:
private void OnChecked(object sender, RoutedEventArgs e)
{
CheckBox checkBox = (CheckBox)e.OriginalSource;
DataGridRow dataGridRow = VisualTreeHelpers.FindAncestor<DataGridRow>(checkBox);
DataRowView produit = (DataRowView)dataGridRow.Item;
if (produit[3].Equals(""))
{
MessageBox.Show(
"Vous ne pouvez pas ajouter cette appellation car elle n'était pas créée l'année dernière. Veuillez la créer manuellement.", "Erreur",
MessageBoxButton.OK, MessageBoxImage.Warning);
}
e.Handled = true;
}
是的,谢谢。但我的问题是在显示弹出窗口时进行过滤,我的意思是只有当“oldLibelle”为空且复选框被选中时。有什么想法吗?是的,和e核实一下。它将显示所选项目感谢Martin,但FindAncestor不是VisualTreeHelper的方法…该.NET类称为VisualTreeHelper,末尾不带“s”。带有“s”的VisualTreeHelpers是Rachel Lim编写的自定义类。你可以找到它,如果你按照'这里'链接和我的答案。VisualTreeHelpers在内部使用VisualTreeHelper。我的道歉!非常感谢。稍后我会给你我的反馈-很好用!谢谢@Martin:-!!我只是错过了在这种情况下如何取消选中复选框。。。
private void OnChecked(object sender, RoutedEventArgs e)
{
CheckBox checkBox = (CheckBox)e.OriginalSource;
DataGridRow dataGridRow = VisualTreeHelpers.FindAncestor<DataGridRow>(checkBox);
DataRowView produit = (DataRowView)dataGridRow.Item;
if (produit[3].Equals(""))
{
MessageBox.Show(
"Vous ne pouvez pas ajouter cette appellation car elle n'était pas créée l'année dernière. Veuillez la créer manuellement.", "Erreur",
MessageBoxButton.OK, MessageBoxImage.Warning);
}
e.Handled = true;
}