我想创建一个包含一个组合框和一个内容控件的用户控件.在组合框中进行的选择应确定内容控件将使用的数据模板.我读过这篇文章,它几乎展示了我想要实现的目标.
组合框中填充了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 …Run Code Online (Sandbox Code Playgroud)