【问题标题】:WPF - Getting the value of one a DataGridCell from within DataTemplateSelectorWPF - 从 DataTemplateSelector 中获取一个 DataGridCell 的值
【发布时间】:2010-03-26 20:10:32
【问题描述】:

我将 DataTemplateSelector 与 WPFToolkit DataGrid 一起使用。我想根据同一行上另一个单元格的值来选择一个单元格的编辑模板。

DataTemplateSelector 的 SelectTemplate 方法有两个参数:一个是网格行显示的数据项,另一个是网格单元格。

我想知道的是如何从 SelectTemplate 方法中获取另一个单元格的值。但是,我不确定通过访问单元格的属性来获取此信息的正确方法。

    public class RangeValuesEditTemplateSelector : DataTemplateSelector
{
    public DataTemplate NumberTemplate{get; set;}
    public DataTemplate TextTemplate{get; set;}

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        //TODO: need to find the correct way to populate the if condition below
        DataGridCell theCell = container as DataGridCell;


        if (theCell.something.somethingElse)
        {
            return NumberTemplate;
        }else{
            return TextTemplate;
        }

    }


}

谁能帮忙?

非常感谢。

【问题讨论】:

  • 您已经找到解决方案了吗?

标签: c# wpf datagrid datatemplate datatemplateselector


【解决方案1】:

我认为您需要一个 ItemViewModel。当您创建这些对象时(每行一个); itemViewModel 应该具有在创建时或通过设置器设置的附加属性(根据您的自定义计算)。因此,如果 EachRow 有一个名为“MoreImportantThanNextOne”的属性,您可以在 ItemVM ctor 中设置它,它会为下一行接收一些数据。
然后在 TemplateSelector 覆盖中,您只需访问 ItemVM 的“MoreImportantThanNextOne”属性值即可选择正确的模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-22
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多