开发者

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

I'm using DataTemplateSelector with the WPFToolkit DataGrid. 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.

What I want to know is how to get the value of another cell from within the SelectTemplate method. 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 Number开发者_如何转开发Template;
        }else{
            return TextTemplate;
        }

    }


}

Can anyone help?

Many thanks in advance.

AT


I think you need an 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. 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.
Then in the TemplateSelector override, you can just access the "MoreImportantThanNextOne" property values of the ItemVM to select the right template.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜