Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Can I use a constant for a resource key name?

<DataTemplate x:Key="MyTemplate" DataType="{x:Type l:MyViewModel}">
    <l:MyView />
</DataTemplate>

Is there any way I can replace the "MyTemplate" with a value from a class of mine?

public sealed class MyTemplateSelector : DataTemplateSelector
{
    public const string TemplateName = "MyTemplate";

    //I use the TemplateName const to retrieve the correct DataTemplate
}
like image 642
michael Avatar asked Dec 10 '22 07:12

michael


1 Answers

<DataTemplate x:Key="{x:Static l:MyTemplateSelector.TemplateName}"
              DataType="{x:Type l:MyViewModel}">
    <l:MyView />
</DataTemplate>
like image 196
max Avatar answered Dec 20 '22 09:12

max