WPF:如何设置内容控制的数据模板触发器?

发布于 2024-11-03 06:27:48 字数 1724 浏览 0 评论 0原文

我想创建一个包含一个组合框和一个内容控件的用户控件。在组合框中所做的选择应确定内容控件将使用的数据模板。我读过这篇文章,它几乎说明了我的内容我正在努力实现。

该组合框填充了enum ModelType 值,可以是PersonCompany。如果用户选择Person,则内容控件应使用personTemplate数据模板;和用于 CompanycompanyTemplate

我被内容控件的 XAML 代码困住了。这是我创建的,但我无法使其工作:

<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>

请帮忙,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2024-11-10 06:27:48

我实际上已经让它发挥作用了。 :)

XAML 应该是这样的:

<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>

枚举的值也可以很好地工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文