<DataGridCheckBoxColumn
HeaderStyle="{StaticResource MetroDataGridColumnHeader}"
Binding="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
IsThreeState="True"
CellStyle="{StaticResource CenterCellStyle}"
CanUserReorder="False"
CanUserResize="False" Header="IsChecked">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllServerConnectionChkBx">
<CheckBox
VerticalAlignment="Center"
HorizontalAlignment="Center"
IsChecked="{Binding Path=DataContext.HeaderChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
x:Name="ckbSelectedAllServerConnections"
Checked="UpCheckbox_Checked"
Margin="10,0,5,0"
Unchecked="UpCheckbox_Checked" />
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}" >
<EventSetter Event="Checked" Handler="OnChecked" />
<EventSetter Event="Unchecked" Handler="OnUnChecked"/>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
Asked
Active
Viewed 994 times
1
-
Hello hms, please understand that Stackoverflow is not your code-writing service. If you have issues translating a specific part, you should ask about this part only and include what you already figured out yourself. The presented XAML is certainly not suitable as a question of how to transform into code (as a principle). It contains to many trivial property setters and to many different aspects. – grek40 Mar 09 '17 at 14:16
1 Answers
0
Try this to create the ElementStyle
:
Style style = new Style(typeof(CheckBox));
style.Setters.Add(new EventSetter(CheckBox.CheckedEvent, new RoutedEventHandler(OnChecked)));
style.Setters.Add(new EventSetter(CheckBox.UncheckedEvent, new RoutedEventHandler(OnUnChecked)));
If you want to be able to create the HeaderTemplate
dynamically you could use the XamlReader.Parse
method (without event handlers):
string xaml = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"dtAllServerConnectionChkBx\"><CheckBox VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\" IsChecked=\"{Binding Path=DataContext.HeaderChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}\" x:Name=\"ckbSelectedAllServerConnections\" Margin=\"10,0,5,0\"/></DataTemplate>";
DataTemplate dt = System.Windows.Markup.XamlReader.Parse(xaml) as DataTemplate;
DataGridCheckBoxColumn column = new DataGridCheckBoxColumn();
column.HeaderTemplate = dt;
column.CellStyle = style;
To be able to hook up the event handlers you need to do some hack, like for example creating a custom CheckBox
control as suggested here:
Attaching an Event Handler to a Code Generated DataTemplate
Or you could just define the DataTemplate
in your <Windows.Resources>
element in your XAML markup and reference it like this:
column.HeaderTemplate = Resources["dtAllServerConnectionChkBx"] as DataTemplate;
-
Thank you for your comment and I used your code but unfortunately my issues still is remained. I've attached my code further to see why I am not getting selected rows after checked & unchecked in my dynamic datagrid. – hms Mar 09 '17 at 12:23
-
Attached your code? Where? Also, I fail to see how this related to your original question about how to create the objects programmatically. Please ask a new question if you have a new issue. – mm8 Mar 09 '17 at 16:21
-
@mm8 he had "attached" his code as an answer and it was then removed by someone. – grek40 Mar 10 '17 at 07:05