简体   繁体   English

WPF-从DataTemplateSelector中获取一个DataGridCell的值

[英]WPF - Getting the value of one a DataGridCell from within DataTemplateSelector

I'm using DataTemplateSelector with the WPFToolkit DataGrid. 我在WPFToolkit DataGrid中使用DataTemplateSelector。 I want to select the editing template for one cell based on the value of another cell on the same row. 我想根据同一行中另一个单元格的值为一个单元格选择编辑模板。

The DataTemplateSelector's SelectTemplate method takes two arguments: one is the data item displayed by the grid row, the other is the grid cell. DataTemplateSelector的SelectTemplate方法采用两个参数:一个是网格行显示的数据项,另一个是网格单元格。

What I want to know is how to get the value of another cell from within the SelectTemplate method. 我想知道的是如何从SelectTemplate方法中获取另一个单元格的值。 However, I'm not sure of the correct way to get this information by accessing properties of the cell. 但是,我不确定通过访问单元格的属性来获取此信息的正确方法。

    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;
        }

    }


}

Can anyone help? 有人可以帮忙吗?

Many thanks in advance. 提前谢谢了。

AT

I think you need an ItemViewModel. 我认为您需要一个ItemViewModel。 When you create these objects (one for each row); 创建这些对象时(每行一个); The itemViewModel should have additional properties that are set (as per your custom computation) on creation or via setters. itemViewModel应该具有在创建时或通过设置器设置(根据您的自定义计算)的其他属性。 so if EachRow has a property called "MoreImportantThanNextOne", you can set it in the ItemVM ctor, which takes in some data for the next row. 因此,如果EveryRow具有一个名为“ MoreImportantThanNextOne”的属性,则可以在ItemVM ctor中进行设置,该属性将为下一行获取一些数据。
Then in the TemplateSelector override, you can just access the "MoreImportantThanNextOne" property values of the ItemVM to select the right template. 然后,在TemplateSelector覆盖中,您可以仅访问ItemVM的“ MoreImportantThanNextOne”属性值以选择正确的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM