简体   繁体   English

WPF数据网格视图默认复选框值获取

[英]WPF Data Grid View default Checkbox value get

I am working on a WPF windows application and I need a help to solve some problem in it. 我正在使用WPF Windows应用程序,需要帮助来解决其中的一些问题。

I have a data gridview; 我有一个数据网格视图; here is a XML: 这是XML:

<DataGrid x:Name="MyDataGrid" Margin="0,55.333,8,209.692" Width="273.27">
 <DataGrid.Columns>
                <DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding ID}" Visibility="Hidden" />
                <DataGridTextColumn Header="CopID" IsReadOnly="True" Binding="{Binding CopID}" Visibility="Hidden"/>
                <DataGridTextColumn Header="CgID" IsReadOnly="True" Binding="{Binding CgID}" Visibility="Hidden"/>
                <DataGridTextColumn Header="Item Names" Width="190" IsReadOnly="True" Binding="{Binding ItemName}"/>
                <DataGridCheckBoxColumn  Header="Select" CanUserReorder="False" />
            </DataGrid.Columns>
</DataGrid>

I want to check DataGridCheckBoxColumn CheckBox is true or false in loop My hope is this to read a Column Value when user click on button; 我想在循环中检查DataGridCheckBoxColumn CheckBox是true还是false我希望这是当用户单击按钮时读取Column Value的结果。 here is the code: 这是代码:

for (int i = 0; i < MyDataGrid.Items.Count - 1; i++)
{
    /*Some code here for Check CheckBox True or False*/
    for (int j = 0; j < MyDataGrid.Columns.Count; j++)
    {      
      string Massage = (MyDataGrid.Items[j] as DataRowView).Row.ItemArray[3].ToString();
      MessageBox.Show( Massage);
     }
 }

I want to check CheckBox is true or false in the loop to get value of that row Columns where is CheckBox is checked I hope somebody can help me to compete my code which I have done give up. 我想在循环中检查CheckBox是对还是错,以获取在其中检查CheckBox行列的值,希望有人可以帮助我竞争我放弃的代码。 I want this code to work when user click on button because I have to save record with this loop in database where CheckBox is true in MyDataGrid . 我希望该代码在用户单击按钮时起作用,因为我必须将此循环保存记录到MyDataGridCheckBox为true的数据库中。

And I also want to select all check box of datagrid when I will check the CheckBox call Select All. 当我将检查CheckBox调用Select All时,我也想选中datagrid的所有复选框。 I hope some buddy help me in it as soon as possible Thank You 我希望一些伙伴能尽快帮助我,谢谢

I'll start by saying that typically, you shouldn't do that, and that's why it's more difficult then you expect. 我首先要说的是,通常不应该这样做,这就是为什么比您期望的要困难的原因。 You should usually access the underlying DataSource instead of the Cells in the DataGrid itself. 通常,您应该访问基础数据源,而不是数据网格本身中的单元。

That said, it is possible. 也就是说,这可能的。 For example: 例如:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
        const int CHECKBOX_COLUMN = 4;
        for (int i = 0; i < MyDataGrid.Items.Count - 1; i++)
        {

            DataGridCell cell = GetCell(i, CHECKBOX_COLUMN);
            CheckBox tb = cell.Content as CheckBox;
            MessageBox.Show(tb.IsChecked.ToString());
        }
}

Where the function GetCell() (and the inner functions that GetCell uses) can be found here 这里可以找到函数GetCell() (以及GetCell使用的内部函数)

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

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