c# - DataGrid 项目无法识别 DataTemplate
问题描述
我尝试使用 DataGrid 控件在 WPF 中创建类似矩阵的视图。我使用通过转换器可用的 DataTable 填充我的项目源列表。每行都是一个自定义类类型 (MatrixCell) 的数组。在 xaml 中,在我的资源选项卡中,我为我的类型 MatrixCell 创建了一个 DataTemplate,但是在运行应用程序时它没有被应用。有谁知道我做错了什么?
我的数据网格:
<DataGrid
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserSortColumns="False"
CanUserResizeColumns="False"
SelectionUnit="Cell"
IsReadOnly="True"
Name="matrixGrid">
<DataGrid.ItemsSource>
<Binding Path="Test" Converter="{StaticResource MatrixToDataViewConverter}"/>
</DataGrid.ItemsSource>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
<Setter Property="LayoutTransform">
<Setter.Value>
<TransformGroup>
<RotateTransform Angle="270"/>
</TransformGroup>
</Setter.Value>
</Setter>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
我的转换器:
public class MatrixToDataViewConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var myDataTable = new System.Data.DataTable();
if(value != null)
{
var test = value as ITest;
myDataTable.Columns.Add("-");
foreach (var column in test.Columns)
{
//populate columns
//...
}
var rows = new List<List<MatrixCell>>();
foreach (var matrixRow in test.MatrixRows)
{
var row = new List<MatrixCell>();
row.Add(new MatrixCell() { Content = $"{matrixRow.Id} {matrixRow.Name}" });
var temp = new MatrixCell[1 + test.Columns.Count];
foreach (var column in test.Columns)
{
//determine the Content value for each MatrixCell
//...
row.Add(new MatrixCell() { Content = content });
}
row.CopyTo(temp);
myDataTable.Rows.Add(temp);
}
}
return myDataTable.DefaultView;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
矩阵单元类:
public class MatrixCell
{
public string Content { get; set; }
public bool IsComparerDone { get; set; }
//public override string ToString()
//{
// return Content;
//}
}
数据模板:
<DataTemplate DataType="{x:Type viewModels:MatrixCell}">
<TextBlock Text="{Binding Content}">
<!--<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IsComparerResult}" Value="True">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>-->
</TextBlock>
</DataTemplate>
只是为了显示 MatrixCell.Content 我可以覆盖 ToString 方法并且它可以工作,但我想使用 DataTemplate 来做到这一点。
这就是我当前的应用程序的样子: matrix view
解决方案
欢迎来到 SO!如果您提供MVCE以供将来参考,您将获得更好的答案,但我会尽力提供帮助......
你不需要那个转换器。只需直接绑定到您的列表并显式声明 DataGridTemplateColumn 类型的列。然后在那个地方 aContentControl
并将它的Content
属性绑定到它当前的 DataContext (即Content="{Binding}"
)。ContentControl 然后会选择您的 DataTemplate 并相应地填充自己。
推荐阅读
- amazon-web-services - AWS S3 StringLike 条件阻止对存储桶的请求
- javascript - Async-Await:即使一个错误,如何在多个等待调用中获取数据?
- sql - oracle在表中找到最后一行
- amazon-sagemaker - 参数验证失败:\n端点中参数 Body 的类型无效
- c - 编译包含 aarch64 的 C 库的 rust crate 时遇到问题
- java - Chronicle Queue 使用零分配从队列读取/写入
- java - 当我选择图像时它不会出现
- javascript - 如何在 chrome 扩展中保存值?
- objective-c - 检查所有相关的 Xcode 设置后,我的可执行文件仍然显示“已通过优化编译”。有什么建议吗?
- amazon-web-services - 将 AWS cli 命令与 xargs 结合使用不起作用