我想创建一个包含一个组合框和一个内容控件的用户控件.在组合框中进行的选择应确定内容控件将使用的数据模板.我读过这篇文章,它几乎展示了我想要实现的目标.
组合框中填充了enum ModelType
值,可以是Person
或Company
.如果用户选择Person
,内容控件应使用personTemplate
数据模板; 并companyTemplate
为Company
.
我对内容控件的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>
Run Code Online (Sandbox Code Playgroud)
请帮忙,谢谢.
Bor*_*ris 87
我实际上得到了它的工作.:)
这是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>
Run Code Online (Sandbox Code Playgroud)
枚举的值也很好.我希望这可以帮助一些有需要的人.
归档时间: |
|
查看次数: |
42260 次 |
最近记录: |