我有一个名为“SingleRow”的CustomControl
自定义属性( )。
如果该属性设置为 true 我需要使用特定的样式。
如果该属性设置为 false 我需要使用默认样式 - 为此我不需要做任何特别的事情。DependencyProperty
这是我的风格:
<Style TargetType="{x:Type local:MetroTabControl}">
...
</Style>
<Style x:Key="MetroTabControlSingleRow" TargetType="{x:Type local:MetroTabControl}">
...
</Style>
当 SingleRow 属性为真时,如何设置CustomControl
使用“MetroTabControlSingleRow”?
我试过了:
public static DependencyProperty SingleRowPropertyKey = DependencyProperty.Register("SingleRow", typeof(bool), typeof(MetroTabControl), new PropertyMetadata(true));
public bool SingleRow
{
get { return (bool)GetValue(SingleRowPropertyKey); }
set { base.SetValue(SingleRowPropertyKey, value); }
}
public override void OnApplyTemplate()
{
if (SingleRow)
{
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("/MetroControls;component/Generic.xaml", System.UriKind.Relative);
Resources.MergedDictionaries.Add(rd);
SetResourceReference(MetroTabControl.StyleProperty, "MetroTabControlSingleRow");
Style = (Style)this.FindResource("MetroTabControlSingleRow");
}
}
但它一直在崩溃。
编辑: 另外,根据开发刺猬的评论,我试过:
<Style TargetType="{x:Type local:MetroTabControl}">
<Style.Triggers>
<Trigger Property="SingleRow" Value="True">
<Setter Property="Template" Value="{StaticResource MetroTabControlSingleRow}" />
</Trigger>
<Trigger Property="SingleRow" Value="False">
<Setter Property="Template" Value="{StaticResource MetroTabControlMultiRows}" />
</Trigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="MetroTabControlSingleRow" TargetType="{x:Type local:MetroTabControl}">
...
</ControlTemplate>
<ControlTemplate x:Key="MetroTabControlMultiRows" TargetType="{x:Type local:MetroTabControl}">
...
</ControlTemplate>
并且仍然崩溃。
感谢您的帮助。