【问题标题】:wpf default style and style basewpf 默认样式和样式库
【发布时间】:2014-03-18 21:10:20
【问题描述】:

我正在尝试制作一种 wpf 样式,以使我能够: 1. 基于基本样式的替代样式 2. 将基本样式设为每个控件在未应用样式时使用的默认样式。

例如,我有代码为按钮制作样式...

<!--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”属性上设置。只能在 DependencyObject 的 DependencyProperty 上设置“DynamicResourceExtension”。

【问题讨论】:

  • 究竟是哪个控件?标签包含其他控件,在设置样式时必须在其中处理这些控件。尝试使用 TextBlock 而不是 Label 作为其原子。
  • 现在,图像、文本块、标签、组合框、复选框......它不断地继续不起作用。我宁愿不发布我所有的代码,所以我在上面包含了一个示例。
  • 它可以帮助启用 WPF 跟踪 msdn.microsoft.com/en-us/library/dd409960.aspx
  • 看我的例子。它只是工作。

标签: c# wpf xaml styles


【解决方案1】:

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

TargetType="Button"

您可以尝试创建一个以父 FrameworkElement 为目标的样式。请注意,并非所有属性都受支持。某些属性是子控件独有的。

【讨论】:

  • 我正在这样做。我添加了一个在上面不起作用的示例。
  • 你能解释一下“不起作用”是什么意思吗?如果在您的示例中,您指的是对齐属性没有被继承,那实际上可能不是真的。对齐也受任何父容器的影响。您的图像样式将使其与其父级的顶部和右侧对齐,但该父级也很可能有一个父级并与其父级的底部和左侧对齐
  • 是的,我会建议他改变颜色进行测试。
  • 我无法执行。它抛出一个异常。给出错误提示:“DynamicResourceExtension”不能在“Style”类型的“BasedOn”属性上设置。只能在 DependencyObject 的 DependencyProperty 上设置“DynamicResourceExtension”。
【解决方案2】:

它对我有用,刚刚测试过。

<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>

编辑:添加派生的右对齐以获得乐趣。

【讨论】:

  • 这不是我想要做的。两个标签都具有有效的样式,但我想做的是设置它,以便每个标签都会自动继承样式,而不必指定它是 BaseLabel。
  • 我在哪里明确指定了标签上的样式?
  • 哎呀,我想我错过了。我发现出了什么问题。我使用了 BasedOn="DynamicResource" 而不是 StaticResource 的错字。再次感谢!
猜你喜欢
  • 2015-02-28
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多