The RowHeaderTemplateSelector object parameter is null

Hi I am using WpfToolKit DataGrid and want to set RowHeaderTemplate Dynamically dependent on element type and in my code the object parameter is always zero 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}">

      

FROM#

 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;
    }
}

      

+3


source to share


1 answer


I know this is an old post, but hope this helps someone!

This is what Microsoft says:

RowHeaderTemplate does not inherit data context from DataGrid.

The documentation goes on to say:

To customize the content of the row header to display values ​​based on the row data, bind the Content property to the DataGridRowHeader property 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>

      

The object parameter should now return the element bound to the DataGridRow.

+3


source







All Articles