WPF:如何设置内容控制的数据模板触发器?
我想创建一个包含一个组合框和一个内容控件的用户控件。在组合框中所做的选择应确定内容控件将使用的数据模板。我读过这篇文章,它几乎说明了我的内容我正在努力实现。
该组合框填充了enum 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>
请帮忙,谢谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上已经让它发挥作用了。 :)
XAML 应该是这样的:
枚举的值也可以很好地工作。
I actually got it to work. :)
Here's what the XAML is supposed to look like:
The values of the enum also work well.