Setting CheckBox as UWP DataGrid column header during AutoGeneratingColumn

BitSmithy 1,956 Reputation points
2024-05-22T14:42:09.97+00:00

Hello,

I want to set Community Toolkit DataGrid column header as CheckBox.

I want to do it in AutoGeneratingColum event. I am trying such code:

        private void AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 		
{ 
			if(e.PropertyName == "IsSelected")
			{
                var templateColumn = new DataGridTemplateColumn();
				templateColumn.Header = new CheckBox();

                var template =	(DataTemplate)this.Resources["dataTemplate"];
                templateColumn.CellTemplate = template;
                e.Column = templateColumn;

			}
}

But the code line "templateColumn.Header = new CheckBox();" causes exception.

Please show me how to do my task.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 16,391 Reputation points Microsoft Vendor
    2024-05-23T07:19:12.77+00:00

    Hi @BitSmithy ,

    Here is a workaround. It is hard to set ControlTemplate form code behind, wo can try to create a new style of DataGridColumnHeader in Xaml resource and use the style in event AutoGeneratingColumn.

       // xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
       // xmlns:wctprimitives="using:Microsoft.Toolkit.Uwp.UI.Controls.Primitives"
        <Grid>
            <Grid.Resources>
                <Style x:Name="dataGridcheckbostyle" TargetType="wctprimitives:DataGridColumnHeader">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="wctprimitives:DataGridColumnHeader">
                                <CheckBox Content="Test" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Grid.Resources>
            <controls:DataGrid x:Name="dataGrid1" 
                    Height="600" Margin="12"
                    AutoGenerateColumns="True"
                    AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn"
                    ItemsSource="{x:Bind CustomerList}">
                
            </controls:DataGrid>
        </Grid>
    
    
    
     public sealed partial class MainPage : Page
     {
         public List<Customer> CustomerList= new List<Customer>();
         public MainPage()
         {
             this.InitializeComponent();
             CustomerList = new List<Customer> {
                 new Customer("A.", "Zero",
                 "12 North Third Street, Apartment 45",
                 false),
             new Customer("B.", "One",
                 "34 West Fifth Street, Apartment 67",
                 false),
             new Customer("C.", "Two",
                 "56 East Seventh Street, Apartment 89",
                 true),
             new Customer("D.", "Three",
                 "78 South Ninth Street, Apartment 10",
                 true)
             };
         
             
         }
         private void dataGrid1_AutoGeneratingColumn(object sender, Microsoft.Toolkit.Uwp.UI.Controls.DataGridAutoGeneratingColumnEventArgs e)
         {
             if (e.PropertyName == "IsNew")
             {
                 
                 e.Column.HeaderStyle = dataGridcheckbostyle;
             }
         }
     }
    
     public class Customer
     {
         public String FirstName { get; set; }
         public String LastName { get; set; }
         public String Address { get; set; }
         public Boolean IsNew { get; set; }
         public Customer(String firstName, String lastName,
            String address, Boolean isNew)
         {
             this.FirstName = firstName;
             this.LastName = lastName;
             this.Address = address;
             this.IsNew = isNew;
         }
     }
    
    
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful