本文介绍了带DataTrigger的ControlTemplate.具有DataTemplateSelector的DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用控件,该控件根据ViewModel内部的type属性显示一个编辑器.目前,它是使用ControlControlTemplateDataTrigger这样实现的-

I have a generic control which displays an editor based on the type property inside a ViewModel. Currently it's implemented using Control, ControlTemplate and DataTrigger like this -

<Control
   x:Name="MainControl"
   Grid.Column="1"
   TargetUpdated="OnTargetUpdated">
        <Control.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger
                        Binding="{Binding Path=EditorType}"
                        Value="{x:Static view:EditorType.Bool}">
                        <Setter
                            Property="Control.Template"
                            Value="{StaticResource boolTemplate}" />
                    </DataTrigger>
                    <DataTrigger
                        Binding="{Binding Path=EditorType}"
                        Value="{x:Static view:EditorType.Text}">
                        <Setter
                            Property="Control.Template"
                            Value="{StaticResource textTemplate}" />
                    </DataTrigger>
                    <DataTrigger
                        Binding="{Binding Path=EditorType}"
                        Value="{x:Static view:EditorType.Integer}">
                        <Setter
                            Property="Control.Template"
                            Value="{StaticResource integerTemplate}" />
                    </DataTrigger>
                    ...
                    ....
                </Style.Triggers>
            </Style>
     </Control.Style>
</Control>

现在,可以使用ContentPresenterDataTemplateDataTemplateSelector这样实现相同的功能-

Now, same can be achieved using ContentPresenter, DataTemplate and DataTemplateSelector like this -

<local:EditorTemplateSelector
    BoolEditorTemplate="{StaticResource boolTemplate}"
    TextEditorTemplate="{StaticResource textTemplate}"
    IntegerEditorTemplate="{StaticResource integerTemplate}"
    ...
    ....
    x:Key="EditorTemplateSelector">
</local:EditorTemplateSelector>

<ContentPresenter
    ContentTemplateSelector="{Binding Source={StaticResource EditorTemplateSelector}}"
    Content="{Binding}"
    TargetUpdated="OnTargetUpdated">
</ContentPresenter>

// Template selector returning appropriate template based on type

我觉得使用DataTemplateSelector的第二种方法更好,但想向您了解-

I feel the second approach, using DataTemplateSelector is better but would like to know from you -

  • 哪个更好?为什么?

  • Which one is better and why?

两者的性能会有差异吗?

Will there be any performance difference in two?

推荐答案

我听说DataTemplateSelectors如果基于更改的值不会更新模板,因此我通常不使用他们.

I've heard that DataTemplateSelectors do not update the template if the value they're based on changes, and because of this I usually don't use them.

我首选的方法实际上是使用DataTemplates.

My preferred method is actually to use DataTemplates.

<MyControl.Resources>
    <DataTemplate TargetType="{x:Type local:BooleanModel}">
        <local:BooleanView />
    </DataTemplate>
    <DataTemplate TargetType="{x:Type local:IntegerModel}">
        <local:IntegerView />
    </DataTemplate>
    ...
</MyControl.Resources>

第二,如果要基于属性而不是对象类型更改模板,则倾向于使用DataTriggers.这是因为如果该属性曾经更改过,PropertyChange通知将自动告诉UI它已更改并更新模板.我不相信DataTemplateSelectors会自动执行此操作.我还更喜欢在XAML中看到模板选择逻辑,而不是将其隐藏在TemplateSelector文件中,但这只是个人喜好.

Second, if I want to change the template based on a property rather than the object type, I tend to use DataTriggers. This is because if that property ever gets changed, the PropertyChange notification will automatically tell the UI that it has changed and to update the template. I do not believe DataTemplateSelectors do this automatically. I also prefer to see the template selection logic in my XAML, not have it hidden in a TemplateSelector file, but that's just personal preference.

最后我的选择是使用DataTemplateSelector.我几乎从未在WPF应用程序中使用过,尽管我经常在Silverlight中使用它,因为它不支持我使用隐式DataTemplates(尚未)

And my last choice is to use a DataTemplateSelector. I almost never use one in a WPF application, although I often do in Silverlight since it doesn't support my preferred method of using implicit DataTemplates (yet)

我不知道两者之间的任何显着性能差异,尽管如果有人可以告诉我,我会很感兴趣.

I am not aware of any significant performance differences between the two, although I would be interested if someone can tell me otherwise.

这篇关于带DataTrigger的ControlTemplate.具有DataTemplateSelector的DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:13