我想创建一个包含一个组合框和内容控件的用户控件。在组合框中选择的选项应该确定内容控件使用的数据模板。我阅读了 这篇文章,它几乎展示了我要实现的内容。
组合框填充了枚举型 ModelType 的值,可以是 Person 或 Company。如果用户选择 Person,则内容控件应使用 personTemplate 数据模板;对于 Company 则使用 companyTemplate。
我在内容控件的 XAML 代码中卡住了。这里是我创建的内容,但我无法使其工作:
组合框填充了枚举型 ModelType 的值,可以是 Person 或 Company。如果用户选择 Person,则内容控件应使用 personTemplate 数据模板;对于 Company 则使用 companyTemplate。
我在内容控件的 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>
请帮忙,谢谢。