简体   繁体   English

获取WPF DataGrid中的Checkbox值

[英]Get the Checkbox value in wpf datagrid

i have a datagrid in wpf, i have multiple rows(items) in that datagrid and have one checkbox column in each row. 我在wpf中有一个数据网格,在该数据网格中有多个行(项目),并且每行中都有一个复选框列。 i want to check in all rows if the checkbox is checked in any row then perform action below is my code. 我想检查所有行,如果在任何行中都选中了复选框,则执行以下操作是我的代码。 Thanks! 谢谢!

WPF Code WPF代码

<DataGrid CanUserAddRows="False" AutoGenerateColumns="False"
                  CellEditEnding="SaveDeliveryValue" LoadingRow="DataGrid_LoadingRow"
                  Name="ViewOrdersGrid" HorizontalAlignment="Center" Margin="0,10,0,0" 
                  VerticalAlignment="Top" Height="278" Width="520" BorderBrush="#FFA0A0A0">
            <DataGrid.Columns>
                <DataGridTextColumn  Header="Order No" Width="115" Binding="{Binding Path=BONo, Mode=OneWay}" />
                <DataGridTextColumn Header="Order Date" Width="100" Binding="{Binding Path=BODate, Mode=OneWay, StringFormat=d}" />
                <DataGridTextColumn Header="Total Amount" Width="100" Binding="{Binding Path=BOTotal, Mode=OneWay}" />
                <DataGridTextColumn Header="Total Bikes" Width="100" Binding="{Binding Path=BOTatalBikes, Mode=OneWay}" />
                <DataGridCheckBoxColumn Header="Delivered" x:Name="DeliveryValueCheck" Width="70" Binding="{Binding Path=BODelivered, Mode=TwoWay}" />
            </DataGrid.Columns>
        </DataGrid>

C# code C#代码

private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            for (int i = 0; i < ViewOrdersGrid.Items.Count; i++)
            {
                CheckBox mycheckbox = ViewOrdersGrid.Columns[4].GetCellContent(ViewOrdersGrid.Items[i]) as CheckBox;
                if (mycheckbox.IsChecked == true)
                {
                    MessageBox.Show("Checked");
                }

            }
        }

You're already using MVVM, I can see by the bindings, so you're off to a good start. 通过绑定可以看到您已经在使用MVVM,因此您的开端很好。 Now, because MVVM allows a very tight relationship between the UI and the data, we can inference that if we can traverse the visual tree for the checked property on a given object, we should also be able to traverse the data for such a property. 现在,由于MVVM允许UI与数据之间建立非常紧密的关系,因此我们可以推断出,如果可以遍历给定对象上已检查属性的可视树,那么我们也应该能够遍历此类属性的数据。 So your C# code should look as follows (assuming in your code that DataGrid's ItemsSource is bound to a collection (lets call it MyItems): 因此,您的C#代码应如下所示(假设您的代码中DataGrid的ItemsSource已绑定到一个集合(我们将其称为MyItems):

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var viewModel = (ViewModelType)this.DataContext;

    foreach(var item in viewModel.MyItems)
    {
        if(item.BODelivered)
        {
            MessageBox.Show("Checked");
        }
    }
}

This example assumes that (because the rest of your example is using bindings appropriately) that your grid is bound to something (we called it MyItems). 本示例假定(因为本示例的其余部分适当地使用了绑定)网格已绑定到某些对象(我们称为MyItems)。 If you need to see how this works (meaning you haven't implemented it as MVVM and FOOLED ME ) then consider the following: 如果您需要查看其工作原理(这意味着您尚未将其实现为MVVM和FOOLED ME ),请考虑以下事项:

This is your XAML 这是您的XAML

<DataGrid CanUserAddRows="False" AutoGenerateColumns="False"
          CellEditEnding="SaveDeliveryValue" LoadingRow="DataGrid_LoadingRow"
          Name="ViewOrdersGrid" HorizontalAlignment="Center" Margin="0,10,0,0" 
          VerticalAlignment="Top" Height="278" Width="520" BorderBrush="#FFA0A0A0"
          ItemsSource="{Binding MyItems}">
    <DataGrid.Columns>
        <DataGridTextColumn  Header="Order No" Width="115" Binding="{Binding Path=BONo, Mode=OneWay}" />
        <DataGridTextColumn Header="Order Date" Width="100" Binding="{Binding Path=BODate, Mode=OneWay, StringFormat=d}" />
        <DataGridTextColumn Header="Total Amount" Width="100" Binding="{Binding Path=BOTotal, Mode=OneWay}" />
        <DataGridTextColumn Header="Total Bikes" Width="100" Binding="{Binding Path=BOTatalBikes, Mode=OneWay}" />
        <DataGridCheckBoxColumn Header="Delivered" x:Name="DeliveryValueCheck" Width="70" Binding="{Binding Path=BODelivered, Mode=TwoWay}" />
    </DataGrid.Columns>
</DataGrid>

This is your data structure 这是你的数据结构

public class MyObject
{
    public int BONo { get; set; }
    public DateTime BODate { get; set; }
    public int BOTotal { get; set; }
    public int BOTatalBikes { get; set; }
    public bool BODelivered { get; set; }
}

This is your *.xaml.cs file 这是您的* .xaml.cs文件

// this is the constructor for your view (MyWindow.xaml.cs)
private MyWindow( )
{
    this.DataContext = new MyViewModel( );
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var viewModel = (ViewModelType)this.DataContext;

    foreach(var item in viewModel.MyItems)
    {
        if(item.BODelivered)
        {
            MessageBox.Show("Checked");
        }
    }
}

This is your view model (MyViewModel.cs) 这是您的视图模型(MyViewModel.cs)

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void OnPropertyChanged(string property)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    private ObservableCollection<YourObjectTypeHere> _myItems;
    public ObservableCollection<YourObjectTypeHere> MyItems
    {
        get
        {
            return _myItems;
        }
        set
        {
            _myItems = value;
            OnPropertyChanged("MyItems");
        }
    }
}

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

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