【发布时间】:2013-05-02 15:02:17
【问题描述】:
我经常使用依赖于本地类类型的触发器。常见的情况是通用控件样式,它们具有依赖于类的 ContentControl。 场景:
1) 实现列表框“MyUserControl”的用户控件
2) 一种大型列表框样式,适用于用户控件内的列表框控件。该样式有一个最初为空的 ContentControl(填充有 Label / StackPanel)
<ContentControl Name="SpecificLabel"></ContentControl>
3) 为每个特定的派生类定义了 Contentcontrols
<ControlTemplate x:Key="listbox1Template">
<Label...
</ControlTemplate>
<ControlTemplate x:Key="listbox2Template">
<StackPanel...
</ControlTemplate>
4) 根据MyListboxControl的派生类,Datatriggers选择contentcontrol的模板:
<DataTrigger Binding="{Binding ElementName=MyUserControl, Path=DataType}" Value="{x:Type local:MyListbox1}">
<Setter TargetName="SpecificLabel" Property="Template" Value="{StaticResource listbox1template}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=MyUserControl, Path=DataType}" Value="{x:Type local:MyListbox2}">
<Setter TargetName="SpecificLabel" Property="Template" Value="{StaticResource listbox2template}"/>
</DataTrigger>
因此,基类提供了一个 DataType 属性:
public Type DataType
{
get
{
return this.GetType();
}
}
这看起来一定有更简单的方法。是否可以仅在 XAML 中获取本地类型,而不使用此属性?
【问题讨论】:
标签: wpf triggers datatrigger