Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

wpf - ControlTemplate with DataTrigger Vs. DataTemplate with DataTemplateSelector

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>

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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>

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...