c# - RowHeaderTemplateSelector 对象参数为空

标签 c# xaml datatemplate

您好,我正在使用 WpfToolKit DataGrid 并希望根据项目类型动态设置 RowHeaderTemplate,在我的代码中,对象参数始终为 null 这是我的代码

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

最佳答案

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

这是微软的说法:

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

因此,您需要做的是将类似这样的内容添加到您的数据网格 xaml 中:

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

现在,对象参数应该带回绑定(bind)到 DataGridRow 的项目。

关于c# - RowHeaderTemplateSelector 对象参数为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31461170/

相关文章:

c# - 在多对多关系上隐藏连接实体

c# - 使用 XamlReader 创建的 DataTemplate 无法找到 StaticResources

c# - 停止 TabControl 重新创建其子项

wpf - 无法从 TabControl DataTemplate 获得控件

c# - 我想读取 XML 文件的单个元素并将其输出到 aspx 控件

c# - 在 WebForms 应用程序中读取自定义配置文件

c# - 屏蔽时我可以使用哪些逻辑运算来忽略不相关的位?

c# - 当以编程方式更新 TabItem 可见性时,不会触发 IsVisibleChanged

c# - 如何在 WPF 中隐藏 GridViewColumn,就好像它在运行时折叠一样?

c# - xamDataGrid 过滤器记录样式