0

I am trying to set a global style for multiple control derived types by putting this in my 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}}" />

Right now the window style only works in the visual studio design window and the button style doesn't work at all. What have I done wrong?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Justin
  • 2,399
  • 4
  • 31
  • 50
  • what version of Visual Studio & .NET Framework are you using? I've been having problems getting styles to work with .NET 4.0 (WPF 4.0). See http://stackoverflow.com/questions/4239714/why-cant-i-style-a-control-with-the-aero-theme-applied-in-wpf-4-0. – devuxer Nov 28 '10 at 20:15

1 Answers1

1

I've found sometimes that BasedOn is rather particular. If you assign a key then it tends to work more often. I'm not sure if the value bindings are causing your issue as i didn't make and external static class to use.

<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>
Gauthier
  • 1,251
  • 10
  • 25