Specify a default empty DataTemplate instead of the default 'ToString()' DataTemplate

前端 未结 6 1348
囚心锁ツ
囚心锁ツ 2021-02-19 10:11

The default DataTemplate in a wpf application displays the result of the .ToString() method. I\'m developing an application where the default DataTemplate should di

6条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 10:50

    I know of no way to do this. As per Joe's comment below, WPF specifically disallows specifying a DataTemplate for type Object.

    Depending on your exact requirements, it may be easier to search for a DataTemplate that matches the specific type. If you find one, use it. Otherwise, display nothing. For example:

    
    

    And in your selector (pseudo-code, obviously):

    var dataTemplateKey = new DataTemplateKey() { DataType = theType; };
    var dataTemplate = yourControl.FindResource(dataTemplateKey);
    
    if (dataTemplate != null)
    {
        return dataTemplate;
    }
    
    return NulloDataTemplate;
    

提交回复
热议问题