Combining DataTemplates at runtime

前端 未结 2 854
遇见更好的自我
遇见更好的自我 2020-12-04 03:54

I have a ListBox that presents a databound list of objects via its ItemSource. Because each object has special display needs I’m defining an ItemTemplateSelector that return

相关标签:
2条回答
  • 2020-12-04 04:37

    You could create a new CustomControl that fits your needs. It will apply the style by itself and you can give additional DepdendencyProperties to make it more convinient. In the end you can still put it in a DataTemplate to use it with your DataTemplateSelector.

    0 讨论(0)
  • 2020-12-04 04:43

    You could create the DataTemplate dynamically using the XamlReader.Parse or XamlReader.Load method, e.g.:

    string template = "<DataTemplate xmlns =\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x =\"http://schemas.microsoft.com/winfx/2006/xaml\"><StackPanel>[PLACEHOLDER]</StackPanel></DataTemplate>".Replace("[PLACEHOLDER]", "...custom code...");
    return System.Windows.Markup.XamlReader.Parse(template) as DataTemplate;
    

    The custom parts could be defined as UserControls.

    I am afraid there is no way to base a DataTemplate on another one in pure XAML though.

    0 讨论(0)
提交回复
热议问题