wpf 如何在按钮单击事件上检查 DataGridCheckBoxColumn 复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18145252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:24:16  来源:igfitidea点击:

how to check DataGridCheckBoxColumn checkbox on button click event

c#.netwpfxaml

提问by Mark

How to check the DataGridCheckBoxColumn on button click. So when I fire a button click event I want the DataGridCheckBoxColumn to be checked.

如何在按钮单击时检查 DataGridCheckBoxColumn。因此,当我触发按钮单击事件时,我希望检查 DataGridCheckBoxColumn。

 private void loadCheckBoxColumn()
    {
        MedicationDatagrid.Columns.Add( new DataGridCheckBoxColumn
         {
            Header = "Selected",
            Binding = new Binding( "checkBox" )

           } );
    }

Button Click Event:

按钮点击事件:

 private void btnchecked_Click( object sender, RoutedEventArgs e )
        {

        AwaitingMeds checkedItem = new AwaitingMeds();
        List<AwaitingMeds> awaitingMedslist = MedicationDatagrid.ItemsSource as                   List<AwaitingMeds>;
        buttonClickCheckedUnChecked();
        if( !( ( AwaitingMeds )MedicationDatagrid.SelectedItem == null ) )
        {
            checkedItem.checkBox = true;

        }

    }

buttonClickCheckedUnChecked:

buttonClickCheckedUnChecked:

   private void buttonClickCheckedUnChecked()
    {

        AwaitingMeds checkedItem = new AwaitingMeds();

        if( checkedItem.checkBox == true )
        {


            btnunChecked.Visibility = Visibility.Visible;
        }
        else
        {

            btnunChecked.Visibility = Visibility.Collapsed;
        }
    }

回答by BendEg

I would prefer to use the normal WPF-CkeckBox in a ColumnTemplate. Then simple bind the CheckBox to your ItemSource and go throught it.

我更喜欢在 ColumnTemplate 中使用普通的 WPF-CkeckBox。然后简单地将 CheckBox 绑定到您的 ItemSource 并通过它。

<DataGridTemplateColumn Header="Ist aktiv" IsReadOnly="True">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsActiveBool}" Click="myIsActiveCheckBox_Click" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

回答by Ajay

On button click event

按钮点击事件

static int SelectColumnIndex = 0;
PerformAction_Click(object sender, System.EventArgs e)
{
string data = string.Empty;
foreach(DataGridViewRow row in MyDataGridView.Rows)
{
  if(row.Cells[SelectColumnIndex].Value!=null &&
         Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
  {
    foreach(DataGridViewCell cell in row.Cells)
    {
      if(cell.OwningColumn.Index!= SelectColumnIndex)
      {
        data+= (cell.Value + " ") ; // do some thing
      }
    }
    data+="\n";
  }
 }
 MessageBox.Show(data, "Data");
 }