WPF根据自定义属性的值设置CustomControl样式

问题描述:

我有一个CustomControl自定义属性(DependencyProperty),名为“SingleRow”。
如果该属性设置为true,我需要使用特定的样式。
如果属性设置为false,我需要使用默认样式 - 因为我不需要做任何特殊的事情。WPF根据自定义属性的值设置CustomControl样式

这里是我的风格:

<Style TargetType="{x:Type local:MetroTabControl}"> 
... 
</Style> 

<Style x:Key="MetroTabControlSingleRow" TargetType="{x:Type local:MetroTabControl}"> 
... 
</Style> 

我如何可以设置CustomControl使用 “MetroTabControlSingleRow” 当SingleRow属性为true?

我想:

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> 

和仍然崩溃。

希望你的帮助。

+0

我使用ContentPresenter和转换器,以及contenpresenter的contentemplate绑定属性。 – iulian3000

+0

什么崩溃?你可以说得更详细点吗? –

+0

如何调试设计崩溃?我不能把try {} catch {}赶上例外。 – Ron

只需使用触发器即可。

以下是IsFocused属性的示例。

<ControlTemplate x:Key="NotFocused" TargetType="{x:Type TextBox}"> 
    . . . 
</ControlTemplate> 

<ControlTemplate x:Key="Focused" TargetType="{x:Type TextBox}"> 
    . . . 
</ControlTemplate> 


<Style TargetType="{x:Type TextBox}"> 
    <Style.Triggers> 
     <Trigger Property="IsFocused" Value="True"> 
      <Setter Property="Template" Value="{StaticResource Focused}" /> 
     </Trigger> 
     <Trigger Property="IsFocused" Value="False"> 
      <Setter Property="Template" Value="{StaticResource NotFocused}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

仍然压碎。也许我没有正确定义我的SingleRow DependencyProperty? – Ron

+0

我在互联网上发现了这个代码。它与IsFocused属性一起工作。你将无法复制过去:)但我希望你明白。它会工作。检查拼写和依赖属性定义... –

+0

ofc,我修改它以匹配我的程序 - 请参阅我的编辑 – Ron

这就是我如何使用它,它适用于我。

代码:

public class MyConverter : IValueConverter 
{ 
    public DataTemplate First { get; set; } 
    public DataTemplate Second { get; set; } 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if ((bool)value) 
      return First; 
     else 
      return Second; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

XAML:

<DataTemplate x:Key="first"> 
    <Button>one</Button> 
</DataTemplate> 

<DataTemplate x:Key="second"> 
    <CheckBox/> 
</DataTemplate> 

<local:MyConverter x:Key="myConverter" 
        First="{StaticResource first}" 
        Second="{StaticResource second}" /> 

<Style TargetType="{x:Type local:MyCustomControl1}"> 
    <Setter Property="Template" > 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:MyCustomControl1}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}"> 
        <ContentPresenter ContentTemplate="{Binding SingleRow, Converter={StaticResource myConverter}, RelativeSource={RelativeSource TemplatedParent}}" /> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

您正在绑定您的ContentPresenter,我不想更改ContentPresenter的模板,但其父项 – Ron