使用 WPF 在数据网格中选中复选框时如何获取行值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32435305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get row value when I check checkbox in datagrid with WPF?
提问by Cooxkie
If user checked 'isACreer' and 'oldLibelle' (in same row) is empty, then I show a message box to inform user that is not authorized. How to do that in WPF ?
如果用户选中“isACreer”并且“oldLibelle”(在同一行)为空,那么我会显示一个消息框以通知用户未获得授权。如何在 WPF 中做到这一点?
Here is my wpf code :
这是我的 wpf 代码:
<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>
Here is my C# code :
这是我的 C# 代码:
private void GetProduits()
{
try
{
_produits = new _Produits();
_produitsProduitsTableAdapter = new ProduitsTableAdapter();
_produitsProduitsTableAdapter.Connection = new OleDbConnection(_connectionString);
_produitsProduitsTableAdapter.Fill(_produits.Produits);
}
catch (Exception ex)
{
_loggerComavi.Error("Erreur lors du chargement de la table Produits");
_loggerComavi.Error(ex.Source);
_loggerComavi.Error(ex.Message);
_loggerComavi.Error(ex.StackTrace);
}
}
private void OnChecked(object sender, RoutedEventArgs e)
{
_loggerComavi.Info("OnChecked");
//TODO MessageBox.show()
}
采纳答案by Martin
Try this:
尝试这个:
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;
}
I do not know how the products look like that are bound to the grid which come from your table adapter.
But you should be able to find your oldLibelleproperty somewhere in the produitobject mentioned above.
我不知道绑定到来自您的表格适配器的网格的产品看起来如何。但是您应该能够oldLibelle在上述produit对象中的某处找到您的财产。
Please note that this solution uses the custom VisualTreeHelpersclass (written by Rachel Lim). It can be found here.
I provides the FindAcenstormethod used above.
请注意,此解决方案使用自定义VisualTreeHelpers类(由 Rachel Lim 编写)。可以在这里找到。我提供了FindAcenstor上面使用的方法。
VisualTreeHelpersuses the .NET class VisualTreeHelperinternally.
VisualTreeHelpers在内部使用 .NET 类VisualTreeHelper。
回答by DeshDeep Singh
this will popup a message bix and you can take action on OK press.
这将弹出一个消息 bix,您可以按 OK 采取行动。
MessageBoxResult result = MessageBox.Show("Please enter the empty field to proceed?", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Asterisk);
if (result == MessageBoxResult.OK)
{
}
please try. thankyou.
请尝试。谢谢你。
回答by Cooxkie
Here is full code:
这是完整的代码:
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;
}


