【问题标题】:DataGrid CellTemplate multiple propertiesDataGrid CellTemplate 多个属性
【发布时间】:2013-09-24 10:18:32
【问题描述】:

我有一个带有 CellTemplate 的 DataGrid,我在其中通过 PropertyDescriptors 动态创建列。我正在使用这种方法:http://paulstovell.com/blog/dynamic-datagrid 列生成工作,正确的内容到达正确的单元格。

我的问题在于我将提供给单元格的内容从 ex 更改。 'string' 或 'int' 到包含多个属性的自定义类。 CellTemplate 不会绑定到内容类中的属性。

内容类:

    public class ContentWrapper
    {
        public Color Color{ get; set; }
        public String Text { get; set; }
        public String Comment { get; set; }
    }

单元格模板:

    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid ToolTip="{Binding Comment}"
                              Background="{Binding Color, Converter={StaticResource ColorToBrushConverter}}">
                            <TextBlock Text="{Binding Text}"
                                       VerticalAlignment="Bottom"
                                       HorizontalAlignment="Left"/>                                                                
                            <Polygon Visibility="{Binding Comment, Converter={StaticResource CommentVisibleConverter}, FallbackValue=Hidden}"
                                     HorizontalAlignment="Right"
                                     Points="0,0 6,0 6,6"
                                     VerticalAlignment="Top">
                                <Polygon.Fill>
                                    <SolidColorBrush Color="Red" />
                                </Polygon.Fill>
                            </Polygon>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.Resources>

如何使 CellTemplate 能够支持自定义类并绑定到它的属性? 还是有更简单的方法?

编辑 列生成是这样的:

    private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        var property = e.PropertyDescriptor as Property;
        if (property != null)
        {
            var binding = new Binding() { Path = new PropertyPath(property), Mode = property.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay };
            var dataGridBoundColumn = e.Column as DataGridBoundColumn;
            if (dataGridBoundColumn != null)
                dataGridBoundColumn.Binding = binding;
            else
            {
                var dataGridComboBoxColumn = e.Column as DataGridComboBoxColumn;
                if (dataGridComboBoxColumn != null)
                    dataGridComboBoxColumn.SelectedItemBinding = binding;
            }
        }
    }

【问题讨论】:

    标签: wpf templates binding properties datagrid


    【解决方案1】:

    很好地解决了您的问题。

    我不是要回答你的问题,只是提供一些建议:

    似乎试图从DataGrid ControlTemplate 内部Bind 到您的数据实例属性...这并不是真正的目的。 Template 属性允许我们定义控件的外观。您应该将您的项目Style 和数据Binding 放在定义数据项目如何呈现的ItemsTemplate 中。这是一个重要的区别。

    来自 MSDN:

    ItemsTemplate 属性 - 获取或设置用于显示每个项目的 DataTemplate。

    模板属性 - 获取或设置控件模板。 ControlTemplate 指定控件的外观

    【讨论】:

    • 感谢您的回复。为了能够绑定到集合,我必须使用 DataTemplate,所以这个问题自己解决了。
    【解决方案2】:

    D'oh.. 已经为此工作了 2 天,并在询问我解决问题后 1 小时。

    我将 Column 类型更改为嵌套类型的 DataGridTemplateColumn,手动加载单元格模板和绑定。

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 2021-12-30
      • 2021-03-23
      • 2012-09-15
      • 1970-01-01
      • 2010-10-24
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多