简体   繁体   English

如果在数据网格 C# 中选中复选框,则获取整行值

[英]Getting whole row value if checkbox is checked in datagrid C#

I'm trying to write a code for getting 3 values from textboxes if some of checkboxes in same row is checked.如果选中了同一行中的某些复选框,我正在尝试编写从文本框中获取 3 个值的代码。 Anyone know an easy(or hard) way to do this?任何人都知道一种简单(或困难)的方法来做到这一点?

My datagrid looks like this:我的数据网格如下所示:在此处输入图片说明

I have Load button that finds a file of specific type(XML.config) somewhere in file system, after that I'm calling a method that gets some strings from that file, find substrings of them and put them in 3 separated lists.我有一个加载按钮,它可以在文件系统的某个地方找到一个特定类型的文件(XML.config),之后我调用一个方法,从该文件中获取一些字符串,找到它们的子字符串并将它们放在 3 个单独的列表中。 Those values are in datagrid as Type, MapTo and Name.这些值在数据网格中作为类型、映射到和名称。 I accomplish this by putting all 3 lists in one ObservableCollection and after that I'm sending that ObservalableCollection to datagrid like this:我通过将所有 3 个列表放在一个 ObservableCollection 中来实现这一点,然后我将该 ObservalableCollection 发送到数据网格,如下所示:

ObservableCollection<Tuple<string, string, string>> _obsCollection = new   ObservableCollection<Tuple<string, string, string>>();
public ObservableCollection<Tuple<string, string, string>> MyObsCollection
{
    get { return _obsCollection; }
}
tabela.ItemsSource = _obsCollection;

This is XAML code that shows binding:这是显示绑定的 XAML 代码:

<DataGrid Grid.Column="0" AutoGenerateColumns="False" Height="206" HorizontalAlignment="Left" Margin="12,265,0,0" Name="tabela" VerticalAlignment="Top" Width="556" SelectionChanged="tabela_SelectionChanged" Grid.RowSpan="2" ItemsSource="Binding MyObsCollection">
            <DataGrid.Columns>

                <DataGridTextColumn Header="Type" Width="122" Binding="{Binding Item1}"/>
                <DataGridTextColumn Header="MapTo" Width="122" Binding="{Binding Item2}"/>
                <DataGridTextColumn Header="Name" Width="121" Binding="{Binding Item3}"/>

                <DataGridTemplateColumn Header="Controller">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding DataGridChecked}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Service">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding DataGridChecked}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Injection">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding DataGridChecked}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>

        </DataGrid>

What I'm practically trying to is to accomplish looping thou all 3 columns containing checkboxes to see which of them are selected and if any of 3 in same row is selected then I need to send all 3 string values from that row to some variable.我实际上想要的是完成循环包含复选框的所有 3 列,以查看选择了哪些列,如果选择了同一行中的 3 个中的任何一个,那么我需要将该行中的所有 3 个字符串值发送到某个变量。 Anyone can help me with this.任何人都可以帮助我解决这个问题。 For instance I don't know how to get isSelected property from checkbox in data grid.例如,我不知道如何从数据网格中的复选框中获取 isSelected 属性。 I was doing a lot of researching and all that I was able to find was examples for DataGridView, and almost nothing for DataGrid.我做了很多研究,我能找到的只是 DataGridView 的示例,而 DataGrid 几乎没有。

Instead of using Tuple create your own class, say RowData with all the properties you want to show as columns:不是使用Tuple创建你自己的类,而是说RowData和你想要显示为列的所有属性:

public class RowData: INotifyPropertyChanged
{
    //implement INotifyPropertyChanged

    public string Type { get; set; }
    public string MapTo { get; set; }
    public string Name { get; set; }
    public bool Controller { get; set; }
    public bool Service { get; set; }
    public bool Injection { get; set; }
}

change ObservableCollection to use your type更改ObservableCollection以使用您的类型

public ObservableCollection<RowData> MyObsCollection { get { .... } }

and set AutoGenerateColumns="True" on DataGrid并在DataGrid上设置AutoGenerateColumns="True"

<DataGrid 
   Grid.Column="0" 
   Grid.RowSpan="2" 
   AutoGenerateColumns="True" 
   Height="206" 
   Width="556" 
   HorizontalAlignment="Left" 
   Margin="12,265,0,0" 
   Name="tabela" 
   VerticalAlignment="Top" 
   SelectionChanged="tabela_SelectionChanged" 
   ItemsSource="{Binding MyObsCollection}"/>

and then to get items where any of 3 CheckBoxes is selected you do:然后要获取选择了 3 个CheckBoxes中的任何一个的项目,您可以执行以下操作:

var selectedList = MyObsCollection.Where(n => n.Controller || n.Service || n.Injection).ToList();

To get the Checked item row from DataGrid and add it into list从 DataGrid 获取 Checked 项目行并将其添加到列表中

      List<string>chzone=new List<string>();//Declare it as globally,
      //Note: if DataGrid is Binded as ,Datagrid.Itemsource=dt.defaultview;
      //If datagrid contain the Checkbox, get the checked item row and add it into list
      private void DataGridcheckbox_Checked(object sender, RoutedEventArgs e)
        {

            var checker = sender as CheckBox;           
            if (checker.IsChecked == true)
            {
                var item = checker.DataContext as DataRowView;
                object[] obj = item.Row.ItemArray;//getting entire row
                chzone.Add(obj[0].ToString());//getting row of first column value
            }
            else //This is for Unchecked Scenario
            {
                var item = checker.DataContext as DataRowView;
                object[] obj = item.Row.ItemArray;
                bool res = chzone.Contains(obj[0].ToString());
                if (res == true)
                {
                    chzone.Remove(obj[0].ToString());//this is used to remove item in list
                }
            }

        }

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

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