【问题标题】:How to create DataGrid with items based on datatype如何使用基于数据类型的项目创建 DataGrid
【发布时间】:2013-08-23 03:00:53
【问题描述】:

我想在数据网格上将我的 Itemssource 的不同成员显示为 Tiles,我可以在其中编辑它们,Tiles 上的编辑选项应该基于数据类型,比如如果它是一个枚举,那么一个组合框应该可用并突出显示当前项目,如果它是一个字符串,则应该进行正常编辑。

我的带有枚举值的 Tile 应该看起来像这样,其中 None 是 Enum(Single,None,All) 中的当前值。任何人都可以提供一些示例代码来为此设置 datagrid 单元格模板的样式。我正在使用 MVVM

【问题讨论】:

    标签: c# wpf xaml mvvm wpfdatagrid


    【解决方案1】:

    您可以使用组合框来做到这一点:

     <ComboBox x:Name="FirstVariable" 
                          SelectedItem="{Binding ElementName=RootControl, Path=Request.Analysis.Variables[0]}"
                          ItemsSource="{Binding VariableTypeValues, ElementName=RootControl}"
                          />
    

    public IEnumerable<VariableType> VariableTypeValues
        {
            get
            {
                return Enum.GetValues(typeof(VariableType)).Cast<VariableType>();
            }
        }
    

    获取枚举值。Request.Analysis.Variables[0] 是 VariableType 类型。

    【讨论】:

      【解决方案2】:

      您可以使用 DataGridTemplateColumn 并将 CellEditingTemplateSelector 设置为自定义 DataTemplateSelector,它将根据单元格值数据类型提供特定的编辑模板。 Just found one that matches yours request

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 2011-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-01
        相关资源
        最近更新 更多