编程语言
首页 > 编程语言> > C#-RowHeaderTemplateSelector对象参数为null

C#-RowHeaderTemplateSelector对象参数为null

作者:互联网

嗨,我正在使用WpfToolKit DataGrid并想根据项目类型动态设置RowHeaderTemplate,在我的代码中object参数始终为null
这是我的代码

a

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

解决方法:

我知道这是一岁的帖子,但是希望这对某人有帮助!

微软是这样说的:

The RowHeaderTemplate does not inherit the data context from the
DataGrid.

该文档进一步说:

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.

这是链接:https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.rowheadertemplate(v=vs.110).aspx

因此,您需要做的是在datagrid xaml中添加以下内容:

<DataGrid.RowHeaderStyle>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Content" Value="{Binding}" />
</Style>
</my:DataGrid.RowHeaderStyle>

现在,object参数应该带回绑定到DataGridRow的项目.

标签:datatemplate,xaml,c
来源: https://codeday.me/bug/20191120/2040580.html