WPF-为什么这些样式不起作用?

时间:2010-11-28 20:07:52

标签: wpf styles

我试图通过将它放在我的app.xaml中来为多个控件派生类型设置一个全局样式:

<Style TargetType="{x:Type Control}">
    <Setter Property="Background" Value="{Binding BackgroundBrush, Source={x:Static m:Settings.Instance}, UpdateSourceTrigger=PropertyChanged}" />
    <Setter Property="Foreground" Value="{Binding ForegroundBrush, Source={x:Static m:Settings.Instance}, UpdateSourceTrigger=PropertyChanged}" />
    <Setter Property="BorderBrush" Value="{Binding ForegroundBrush, Source={x:Static m:Settings.Instance}, UpdateSourceTrigger=PropertyChanged}" />
    <Setter Property="UseLayoutRounding" Value="True" />
</Style>

<Style TargetType="{x:Type Window}" BasedOn="{StaticResource {x:Type Control}}" />

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Control}}" />

现在窗口样式仅适用于visual studio设计窗口,按钮样式根本不起作用。我做错了什么?

1 个答案:

答案 0 :(得分:1)

我有时发现BasedOn非常特别。 如果您分配了一个密钥,那么它往往会更频繁地工作。 我不确定值绑定是否导致您的问题,因为我没有使用外部静态类。

<Grid.Resources>
    <Style x:Key="simpleStyle" TargetType="{x:Type Control}">
        <Setter Property="Background" Value="Blue" />
        <Setter Property="Foreground" Value="Yellow" />
        <Setter Property="BorderBrush" Value="CornflowerBlue" />
    </Style>

    <Style TargetType="{x:Type Control}" BasedOn="{StaticResource simpleStyle}" />

        <Style TargetType="{x:Type Window}" BasedOn="{StaticResource simpleStyle}" />

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource simpleStyle}" />
</Grid.Resources>
<Button Height="50" Width="100">
    Hello
</Button>
相关问题