如果用戶選中'isACreer'和'oldLibelle'(在同一行)爲空,那麼我會顯示一個消息框來通知未授權的用戶。如何在WPF中做到這一點?如何在使用WPF選中datagrid中的複選框時獲取行值?
這裏是我的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()
}
感謝Martin,但FindAncestor不是VisualTreeHelpers的一種方法(https://msdn.microsoft.com/fr-fr/library/system.windows.media.visualtreehelper%28v=vs.110% 29.aspx)... – Cooxkie
.NET類在結尾處稱爲「VisualTreeHelper」,但不包含「s」。 「VisualTreeHelpers」(帶有's')是由Rachel Lim編寫的一個自定義類。你可以找到它,如果你按照'這裏'鏈接和我的答案。 「VisualTreeHelpers」在內部使用「VisualTreeHelper」。 – Martin
我的歉意!謝謝。我稍後會給你我的反饋;-) – Cooxkie