简体   繁体   English

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

[英]How to get row value when I check checkbox in datagrid with WPF?

If user checked 'isACreer' and 'oldLibelle' (in same row) is empty, then I show a message box to inform user that is not authorized. 如果用户选中“ isACreer”和“ oldLibelle”(在同一行中)为空,那么我将显示一个消息框,通知用户未授权。 How to do that in WPF ? 如何在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()
    }

Here is a screenshot : 这是屏幕截图: 屏幕截图

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 oldLibelle property somewhere in the produit object mentioned above. 但是您应该能够在上述produit对象中的某个位置找到oldLibelle属性。

Please note that this solution uses the custom VisualTreeHelpers class (written by Rachel Lim). 请注意,此解决方案使用自定义VisualTreeHelpers类(由Rachel Lim编写)。 It can be found here . 可以在这里找到。 I provides the FindAcenstor method used above. 我提供了FindAcenstor使用的FindAcenstor方法。

VisualTreeHelpers uses the .NET class VisualTreeHelper internally. VisualTreeHelpers在内部使用.NET类VisualTreeHelper

this will popup a message bix and you can take action on OK press. 这将弹出一个消息框,您可以在按OK时采取措施。

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

            }

please try. 请试试。 thankyou. 谢谢。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM