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.