2014-03-18 35 views
2

我试图做一个WPF的风格,让我: 1.基替代的风格掀起了基本样式 2.具备基本样式是每一个控件使用时没有默认风格被应用。WPF的默认样式和风格的基础

例如,我有代码,以使样式按钮...

<!--Base Button--> 
<Style TargetType="Button" x:Key="BaseButton"> 
    <Setter Property="Width" Value="auto"/> 
    <Setter Property="MaxWidth" Value="100"/> 
    <Setter Property="Height" Value="auto"/> 
    <Setter Property="MaxHeight" Value="50"/> 

    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Cursor" Value="Hand"/> 
     </Trigger> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Opacity" Value=".85"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
<!--Apply Base to any Button without a style--> 
<Style TargetType="Button" BasedOn="{StaticResource BaseButton}"/> 

,这里是为特定的按钮样式...

<Style TargetType="Button" x:Key="DeleteBtn" BasedOn="{StaticResource BaseButton}"> 
    <Setter Property="Width" Value="100"/> 
    <Setter Property="Height" Value="35"/> 
    <Setter Property="ContentTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <Image Source="(some url)" Height="16" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
        <Label Content="Delete" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
       </StackPanel>      
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

正如你所看到的,我希望能够将“DeleteBtn”样式基于基础,但是也可以将基础设置为屏幕上放置的每个没有特定样式集的按钮的默认基础。目前这只适用于按钮,没有其他控制。上面的例子工作,但我似乎无法做标签,文本框等相同的东西。任何想法或输入将不胜感激:)

这是一个不起作用的示例..

<!--Base Icon--> 
<Style TargetType="Image" x:Key="BaseIcon"> 
    <Setter Property="VerticalAlignment" Value="Top"/> 
    <Setter Property="HorizontalAlignment" Value="Right"/> 
</Style> 
<!--Apply Base to any Icon without a style--> 
<Style TargetType="Image" BasedOn="{DynamicResource BaseIcon}"/> 

它给出了一个错误:'DynamicResourceExtension'不能在'Style'类型的'BasedOn'属性上设置。 'DynamicResourceExtension'只能在DependencyObject的DependencyProperty上设置。

+0

究竟哪个控件?标签包含其他控件,其中的内容在样式化时必须解决。尝试使用TextBlock而不是Label作为其原子。 – BlueM

+0

现在,图像,文本块,标签,组合框,复选框......它继续运行并不起作用。我宁愿不发布我的所有代码,所以我在上面包含了一个例子。 – Kapow36

+0

它可以帮助启用WPF跟踪http://msdn.microsoft.com/en-us/library/dd409960.aspx – BlueM

回答

1

您的示例仅适用于按钮,因为您的“基本”样式仅定位按钮。 即

TargetType="Button" 

您可以尝试创建目标的父FrameworkElement的,而不是一个风格。只是要警告,并不是所有的属性都支持。一些属性对于子控件是唯一的。

+0

我正在那样做。我添加了一个不起作用的例子。 – Kapow36

+0

你能解释一下你的意思是“不起作用”吗?如果在你的例子中,你指的是对齐属性没有被继承,实际上可能并不是这样。对齐也受任何父容器的影响。您的图像样式将使其与父级的顶部和右侧对齐,但是此父级也可以很好地拥有父级并且与其父级的底部和左侧对齐 – failedprogramming

+0

是的我会建议他更改测试的颜色。 – BlueM

1

它适用于我,只是测试它。

<Window.Resources> 
    <Style TargetType="Label" x:Key="BaseLabel"> 
     <Setter Property="Background" Value="Red"/> 
     <Setter Property="HorizontalContentAlignment" Value="Right"/> 
    </Style> 
    <Style TargetType="Label" BasedOn="{StaticResource BaseLabel}"/> 
</Window.Resources> 
<Grid> 
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Bottom" Orientation="Horizontal"> 
     <Label Width="100" Content="TestLabel1"/> 
     <Label Width="100" Content="TestLabel1" Foreground="White"/> 
    </StackPanel> 
</Grid> 

编辑:增加派生的右对齐的乐趣。

enter image description here

+0

这不是我想要做的。两个标签确实具有可行的样式,但我想要做的就是设置它,以便每个标签都会自动继承样式,而不必指定它是BaseLabel。 – Kapow36

+0

我在哪里明确指定了标签上的样式? – BlueM

+0

哎呀,猜我错过了。我找出了什么是错误的。我制作了具有BasedOn =“DynamicResource”而不是StaticResource的拼写错误。再次感谢! – Kapow36