Hi I'm using WpfToolKit DataGrid and want to set the the RowHeaderTemplate Dynamically depends on Item Type and in my code the object parameter is always null here is my code
xaml
  <DataTemplate x:Key="WithCheckBox">
            <Grid>
                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type wpftk:DataGridRow}}}"/>
            </Grid>
     </DataTemplate>
    <viewModel:CheckBoxRowDataTemplate x:Key="CheckBoxRowDataTemplate"/> 
   <wpftk:DataGrid   RowHeaderTemplateSelector="{StaticResource CheckBoxDataDataTemplate}">
c#
 public class CheckBoxRowDataTemplate : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = (FrameworkElement)container;
        if(element != null && item != null & item is Item)
        {
            if(((Item)item).ItemType  != 3 )
            {
                return element.FindResource("WithCheckBox") as DataTemplate;
            }
            else
            {
               return element.FindResource("WithoutCheckBox") as DataTemplate;
            }
        }
        return null;
    }
}
I know it is a year old post, but hoping this helps someone!
This is what Microsoft says:
The RowHeaderTemplate does not inherit the data context from the DataGrid.
The documentation further says:
To set the row header content to display values based on the row data, bind to the Content property of the DataGridRowHeader by using the RowHeaderStyle property.
This is the link: https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.rowheadertemplate(v=vs.110).aspx
So, what you need to do is add something like this to your datagrid xaml:
<DataGrid.RowHeaderStyle>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Content" Value="{Binding}" />
</Style>
</my:DataGrid.RowHeaderStyle>
Now, the object parameter should bring back the item that is bound to DataGridRow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With