I am using the XamDataGrid and adding one column for every day within a date range, hence I don't know the number of columns until the date range has been selected.
To achieve this, I am manually adding unbound columns to the grid.
This achieves the effect: col1, col2, col3, col4, col5
I have a collection which has two properties, Quantity and Production Date, I have bound my columns to the quantity field using the BindingPath property of the Unbound field.
What I wanted to do is add a checkbox to the header of the column, which I achieved using a data template.
<Style x:Key="CheckBoxHeaderStyle" TargetType="{x:Type igWPF:LabelPresenter}"> <Setter Property="Padding" Value="0" /> <Setter Property="Margin" Value="0" />
<Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox Name="HeaderCheckBox" FlowDirection="RightToLeft" IsChecked="{Binding Path=., Converter={StaticResource dateStringToBooleanConverter}}" > <CheckBox.Content> <TextBlock Name="HeaderTextBlock" Text="{Binding}"></TextBlock> </CheckBox.Content> <CheckBox.Style> <Style TargetType="{x:Type CheckBox}"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=HeaderCheckBox, Path=IsChecked}" Value="True"> <Setter Property="Foreground" Value="White" /> </DataTrigger> </Style.Triggers> </Style> </CheckBox.Style> </CheckBox> <StackPanel.Style> <Style TargetType="StackPanel"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=HeaderCheckBox,Path=IsChecked}" Value="True"> <Setter Property="Background" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </StackPanel.Style> </StackPanel> </DataTemplate> </Setter.Value> </Setter> </Style>
The question is: how do I bind any value to the checked property of this data template? I want to be able to set certain columns to be checked when the grid loads and then get the columns that have been checked by the user whilst using the grid.
I cant find any solution for this that works effectively, please help
Hello Raaj,
The DataContext for the CheckBox is the source of the grid when it is bind with “Path=.”. The DataContext of the XamDataGrid can be set to the viewmodel so the CheckBox could be bound to it using the ElementName property. I put together a sample application to illustrate this approach.
Please feel free to let me know if you need additional assistance.
Hi,
Thanks for the response.
Since in my example, the columns are dynamic and each column can have a checkbox in it, I cant bind to a property on the view mode such as IsChecked.
What I have is a collection with two properties, date and isChecked, so when the grid renders, it will go through this collection and bind to the isChecked property of the collection and check or uncheck the checkbox in the headers.
Could you please show me how to do this?