1

I have in my project a xaml grid where I'm using checkbox our requirement is changed now we are constructing datagrid programatically how can I use xaml in csharp. following is the example I have checkbox column in xaml

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
       <CheckBox x:Name="chkHeader" />
            </DataGridTemplateColumn.Header>
                <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                            <CheckBox IsChecked="{Binding IsChecked,   
                           ElementName=chkHeader, Mode=OneWay,
                           UpdateSourceTrigger=PropertyChanged}"  />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

Here programatically I'm creating a grid

foreach (var field in dc.AllColumns)
 {
     var binding = new Binding(".") { Converter = new  
        IndexingDataFieldsConverter(field) };
        AudioPlayBackGrid.Columns.Add(new DataGridTextColumn { Header = 
          field.Field.Description, Binding = binding});
  }

and here I'm injecting check box programatically that I have in xaml so far I have done

    Binding columnbinding = new Binding("IsChecked");
    columnbinding.Mode = BindingMode.OneWay;
    columnbinding.RelativeSource = new RelativeSource
    (RelativeSourceMode.FindAncestor);
    columnbinding.RelativeSource.AncestorType = datagrid.GetType();

    CheckBox chkHeader = new CheckBox();
    chkHeader.Content = "ALL";
    chkHeader.SetBinding(CheckBox.IsCheckedProperty, columnbinding);
    DataGridCheckBoxColumn checkBoxColumn = new   
    DataGridCheckBoxColumn();
    checkBoxColumn.Header = chkHeader;
    checkBoxColumn.Binding = columnbinding;
    AudioPlayBackGrid.Columns.Add(checkBoxColumn);

Somehow I cannot do in csharp what I did in xaml grid any help will be appreciated

1 Answer 1

3

You can try such an approach to add columns programmatically:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    DataGridTextColumn dgTextColumn = new DataGridTextColumn();
    dgTextColumn.Header = "ID";
    dgTextColumn.Binding = new Binding("ID");
    dataGrid1.Columns.Add(dgTextColumn);

    DataGridCheckBoxColumn dgCheckBoxColumn = new DataGridCheckBoxColumn();
    dgCheckBoxColumn.Header = "IsChecked";
    dgCheckBoxColumn.Binding = new Binding("IsChecked");
    dataGrid1.Columns.Add(dgCheckBoxColumn);

    DataGridTextColumn dgTextColumn2 = new DataGridTextColumn();
    dgTextColumn2.Header = "Name";
    dgTextColumn2.Binding = new Binding("Name");
    dataGrid1.Columns.Add(dgTextColumn2);

    dataGrid1.Items.Add(new Item() { ID = 1, Name = "Someone1", IsChecked = true });
    dataGrid1.Items.Add(new Item() { ID = 2, Name = "Someone2", IsChecked = false });
    dataGrid1.Items.Add(new Item() { ID = 3, Name = "Someone3", IsChecked = true });
    dataGrid1.Items.Add(new Item() { ID = 4, Name = "Someone4", IsChecked = false });
}

And Model:

public class Item
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsChecked { get; set; }
}    
Sign up to request clarification or add additional context in comments.

1 Comment

@jayraja feel free to ask any question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.