簡體   English   中英

WPF-從DataTemplateSelector中獲取一個DataGridCell的值

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

我在WPFToolkit DataGrid中使用DataTemplateSelector。 我想根據同一行中另一個單元格的值為一個單元格選擇編輯模板。

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

    }


}

有人可以幫忙嗎?

提前謝謝了。

我認為您需要一個ItemViewModel。 創建這些對象時(每行一個); itemViewModel應該具有在創建時或通過設置器設置(根據您的自定義計算)的其他屬性。 因此,如果EveryRow具有一個名為“ MoreImportantThanNextOne”的屬性,則可以在ItemVM ctor中進行設置,該屬性將為下一行獲取一些數據。
然后,在TemplateSelector覆蓋中,您可以僅訪問ItemVM的“ MoreImportantThanNextOne”屬性值以選擇正確的模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM