首页 > 解决方案 > 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

标签: c#wpfdatatabledatagriddatatemplate

解决方案


欢迎来到 SO!如果您提供MVCE以供将来参考,您将获得更好的答案,但我会尽力提供帮助......

你不需要那个转换器。只需直接绑定到您的列表并显式声明 DataGridTemplateColumn 类型的列。然后在那个地方 aContentControl并将它的Content属性绑定到它当前的 DataContext (即Content="{Binding}")。ContentControl 然后会选择您的 DataTemplate 并相应地填充自己。


推荐阅读