![](/img/trans.png)
[英]How to show a value in combobox when i select a row of datagrid in wpf?
[英]How to get row value when I check checkbox in datagrid with WPF?
如果用戶選中“ isACreer”和“ oldLibelle”(在同一行中)為空,那么我將顯示一個消息框,通知用戶未授權。 如何在WPF中做到這一點?
這是我的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>
這是我的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()
}
嘗試這個:
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
屬性。
請注意,此解決方案使用自定義VisualTreeHelpers
類(由Rachel Lim編寫)。 可以在這里找到。 我提供了FindAcenstor
使用的FindAcenstor
方法。
VisualTreeHelpers
在內部使用.NET類VisualTreeHelper 。
這將彈出一個消息框,您可以在按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);
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.