views:

25

answers:

2

Hi

I had windows xp before and setting the wpf button background always worked but since windows 7 the background is always set to blue. How could i fix this?

+1  A: 

You'll probably need to make your own template for the buttons. (I guess it's just the hover colour which you're struggling with, not the basic colour, which does follow the Background property).

Here's a very simple button template, which might help you get started. BackBrush and ForeBrush will need setting to suit. (And they're the Back and Fore of the rest of the application, so they look backwards in this example)

<Style TargetType="{x:Type Button}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="ClickMode" Value="Press"/>
    <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
    <Setter Property="BorderBrush" Value="#e9dbae" />
    <Setter Property="Foreground" Value="{StaticResource BackBrush}" />
    <Setter Property="Background" Value="{StaticResource ForeBrush}" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <Border
                    x:Name="Border"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"

                        BorderThickness="2"
                    CornerRadius="2"
                        SnapsToDevicePixels="False"
                    RenderTransformOrigin="0.5,0.5"
                    TextBlock.Foreground="{TemplateBinding Foreground}" >
                        <ContentPresenter
                       Margin="2"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       />
                    </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Border" Property="RenderTransform">
                            <Setter.Value>
                                <ScaleTransform ScaleX="0.975" ScaleY="0.975" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#999999"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Will Dean
the property "OverridesDefaultStyle" helped partially, now i have only problem with ismouseover. the background changes for a split second then turns back to blue.
Sys
A: 

For playing with Backgrounds add to Window.Resources (or any other App ResourceDictionary) this style:

<Style TargetType="Button">
            <Setter Property="Background" Value="Black"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Moccasin"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="Gray"/>
                </Trigger>                
            </Style.Triggers>
        </Style>

This works if you don't need to redefine Button template (geometrical appearance of button). Here we aren't specifying Style x:Key then this style will be applyed to all button in specified resource scope. To specify styles with different appearances you should each style mark with x:Key="StyleName" and then in button define style Style="{DynamicResource StyleName}" or Style="{DynamicResource StyleName}"

Eugene Cheverda