Search code examples
c#wpfdatagriditemssource

datagrid check box column clicked


I have a data grid which take the data from an item source dynamic.

one of the field of the item source is bool(check box column) and I want to know when the user is clicking the check box.

which event is it?

xaml code:

<Window x:Class="ProtocolAnalyzer.createByProtocol"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="createByProtocol" Height="506" Width="384">

    <Grid Margin="0,0,2,4">
       <DataGrid x:Name="dataGridTable" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="452" Width="245" SelectedCellsChanged="dataGridTable_SelectedCellsChanged" >
                  <DataGrid.Columns>           
                 </DataGrid.Columns>
       </DataGrid>
       <Button Content="Create" HorizontalAlignment="Left" Margin="275,165,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="Button_Click"/>

     </Grid>
</Window>

C#:

public class TableList
{
    private string _field;

    public string Field
    {
        get { return _field; }
        set { _field = value; }
    }
    private string _val;

    public string Val
    {
        get { return _val; }
        set { _val = value; }
    }

    private bool _calc;

    public bool Calc
    {
        get { return _calc; }
        set { _calc = value; }
    }

}


/// <summary>
/// Interaction logic for createByProtocol.xaml
/// </summary>
public partial class createByProtocol : Window
{
    private ProtocolData.ProtocolData.Protocols _runningProtocol;
    private List<TableList> _Ltbl;
 public createByProtocol(ProtocolData.ProtocolData.Protocols protocol)
    {
        InitializeComponent();
        _runningProtocol = protocol;
        _Ltbl = new List<TableList>();

        buildTable();
    }


    private void buildTable()
    {
        switch (_runningProtocol)
        {
            case  ProtocolData.ProtocolData.Protocols.ZBM:
                TableList tl = new TableList();
                tl.Field = "Length";
                tl.Val = "";
                tl.Calc = false;
                _Ltbl.Add(tl);

                tl = new TableList();
                tl.Field = "OpCode";
                tl.Val = "";                    
                _Ltbl.Add(tl);


                tl = new TableList();
                tl.Field = "REQ_ID";
                tl.Val = "";                    
                _Ltbl.Add(tl);


                tl = new TableList();
                tl.Field = "Message Type";
                tl.Val = "";                    
                _Ltbl.Add(tl);

                tl = new TableList();
                tl.Field = "DATA";
                tl.Val = "";                    
                _Ltbl.Add(tl);

                tl = new TableList();
                tl.Field = "CS";
                tl.Val = "";                    
                _Ltbl.Add(tl);

                tl = new TableList();
                tl.Field = "EOM";
                tl.Val = "0A";                    
                _Ltbl.Add(tl);

                break;
        }

        dataGridTable.ItemsSource = _Ltbl;
    }
 }

Solution

  • It looks like you are using either auto-generated columns or DataGridCheckBoxColumn and so wondering how to check CheckBox is checked/unchecked. You have to handle CheckBox.Click, CheckBox.Checked, CheckBox.Unchecked attached events at DataGrid level.

    <DataGrid  x:Name="DgrdRight" CheckBox.Click="DgrdRight_Click"  CheckBox.Checked="DgrdRight_Checked" CheckBox.Unchecked="DgrdRight_Unchecked"  Grid.Column="1" Grid.Row="0" Margin="10,0,0,0" ColumnWidth="SizeToHeader" AutoGenerateColumns="False">
       <DataGrid.Columns>
          <DataGridTextColumn Header="Title" Binding="{Binding Title}"/>
          <DataGridCheckBoxColumn ... />
       </DataGrid.Columns>
    </DataGrid>
    

    handlers in code :

            private void DgrdRight_Checked(object sender, RoutedEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("Checked");
            }
    
            private void DgrdRight_Unchecked(object sender, RoutedEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("UnChecked");
            }
    
            private void DgrdRight_Click(object sender, RoutedEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("Clicked");
            }