您好,我正在使用 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.
因此,您需要做的是将类似这样的内容添加到您的数据网格 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/