当我使用WPF检查datagrid中的复选框时如何获取行值?

时间:2015-09-07 09:28:40

标签: c# wpf checkbox datagrid

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

以下是截图: Screenshot

2 个答案:

答案 0 :(得分:1)

试试这个:

<div class="item-list">
  <div class="item-list__child-item"></div>
  <div class="item-list__child-item"></div>
  ...
  <div class="item-list__child-item"></div>
</div>

我不知道产品的外观如何绑定到来自表适配器的网格。 但是您应该能够在上面提到的<div class="item-list"> <div class="item-list__child-item"></div> <div class="item-list__child-item"></div> ... <div class="item-list__child-item"></div> </div> <div class="item-list" id="react-component-name"></div> 对象中找到您的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; } 属性。

请注意,此解决方案使用自定义oldLibelle类(由Rachel Lim编写)。它可以找到here。 我提供了上面使用的produit方法。

VisualTreeHelpers在内部使用.NET类VisualTreeHelper

答案 1 :(得分:-1)

这将弹出一条消息bix,你可以按OK按下。

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

            }

请尝试。三江源。