Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

xaml - WPF: How to set the data template trigger for content control?

I want to create a user control that contains one combo box and a content control. The choice made in the combo box should determine the data template the content control would use. I've read this article which pretty much demonstrates what I am trying to achieve.

The combo box is filled with the enum ModelType values, which can be Person or Company. If the user chooses Person, the content control should use the personTemplate data template; and companyTemplate for Company.

I got stuck with the XAML code for the content control. Here is what I have created but I can't make it work:

<UserControl.Resources>
  ...
  <DataTemplate x:Key="personTemplate" ...>
  <DataTemplate x:Key="companyTemplate" ...>
  ...
</UserControl.Resources>
...
<ContentControl x:Name="Account">
  <ContentControl.ContentTemplate>
    <DataTemplate>
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding AccountType}" Value="Person">
        <!-- I doubt the Value property is set correctly. -->
        <!-- It should be a value of an enum ModelType -->
          <Setter 
              TargetName="Account" 
              Property="ContentTemplate" 
              Value="{StaticResource personTemplate}" />
          <!-- The setter is unaware of the target name, i.e. content control -->
        </DataTrigger>
        <DataTrigger Binding="{Binding AccountType}" Value="Company">
          <Setter 
              TargetName="Account" 
              Property="ContentTemplate" 
              Value="{StaticResource companyTemplate}" />
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </ContentControl.ContentTemplate>                    
</ContentControl>

Please help, thanks.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I actually got it to work. :)

Here's what the XAML is supposed to look like:

<ContentControl Content="{Binding}">
  <ContentControl.Style>
    <Style TargetType="ContentControl">
      <Style.Triggers>
        <DataTrigger Binding="{Binding AccountType}" Value="Person">
          <Setter Property="ContentTemplate" Value="{StaticResource personTemplate}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding AccountType}" Value="Company">
          <Setter Property="ContentTemplate" Value="{StaticResource companyTemplate}" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ContentControl.Style>
</ContentControl>

The values of the enum also work well. I hope this helps some people in need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...