2015-09-07 49 views
1

如果用戶選中'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() 
    } 

下面是截圖: Screenshot

回答

1

試試這個:

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編寫)。它可以發現here。 我提供了上面使用的FindAcenstor方法。

VisualTreeHelpers內部使用.NET類VisualTreeHelper

+0

感謝Martin,但FindAncestor不是VisualTreeHelpers的一種方法(https://msdn.microsoft.com/fr-fr/library/system.windows.media.visualtreehelper%28v=vs.110% 29.aspx)... – Cooxkie

+0

.NET類在結尾處稱爲「VisualTreeHelper」,但不包含「s」。 「VisualTreeHelpers」(帶有's')是由Rachel Lim編寫的一個自定義類。你可以找到它,如果你按照'這裏'鏈接和我的答案。 「VisualTreeHelpers」在內部使用「VisualTreeHelper」。 – Martin

+0

我的歉意!謝謝。我稍後會給你我的反饋;-) – Cooxkie

-1

這將彈出一個消息BIX,你可以按OK按鈕即可採取行動。

MessageBoxResult result = MessageBox.Show("Please enter the empty field to proceed?", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Asterisk); 
      if (result == MessageBoxResult.OK) 
      { 

      } 

請嘗試。謝謝。

+0

是謝謝你。但我的問題是過濾時,我彈出顯示,我的意思是隻有當'oldLibelle'是空的複選框被選中。任何想法 ? – Cooxkie

+0

是與e檢查。它會顯示所選項 –

-1

這裏是全碼:

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; 
    } 
相關問題