简体   繁体   中英

RowHeaderTemplateSelector Object Parameter is null

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM