Click here to Skip to main content
15,920,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, everybody.

I want integrated a image define in ressource dictionnary in background button. This button has a style. When the button hasn't style, my image appears. As soon as I put my button style, the image no longer appears.

I don"t understand my error. Can you help me?

Here is a source code for my button style :
XML
<Style x:Key="CtrlButton" TargetType="{x:Type Button}">

        <Setter Property="FontFamily"  Value="Myriad Pro" />
        <Setter Property="FontSize"  Value="14" />        
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Width" Value="90" />
        <Setter Property="Margin" Value="10,20,10,0" /> 
        <Setter Property="Template">
            <Setter.Value>
                
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border   x:Name="ButtonBorder" 
                              CornerRadius="10,10,10,10" 
                              BorderThickness="4,4,4,4" 
                              removed="#00000000"
                              BorderBrush="#FF8F8F8F"
                              RenderTransformOrigin="0.5,0.5"
                    >
                    <ContentPresenter x:Name="ButtonContentPresenter"
                             VerticalAlignment="Bottom"  
                             HorizontalAlignment="Center"
                    />  
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="RenderTransform" TargetName="ButtonBorder">
                                <Setter.Value>
                                    <TransformGroup>
                                        <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
                                    </TransformGroup>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
                
            </Setter.Value>
        </Setter>
    </Style>


Here is a source code for my button:
XML
<Button  Content ="Close" Style="{StaticResource CtrlButton}" Name="btnClose"  Height="96" Width="96" HorizontalAlignment="Right"  Click="btnClose_Click" Margin="0,20,10,0">
                <Button.Background>
                    <VisualBrush Visual="{StaticResource Close}" >
                    </VisualBrush>
                </Button.Background>
            </Button>


Thanks for your help.
best regard.
Posted
Comments
Kenneth Haugland 30-Apr-13 10:22am    
Is the emplate overriding the Backgound or mabye it lies on top of it, so you should perhaps bind the background in the tamplate to you button background?

1 solution

Kenneth is correct, your Style overlays ontop of the original background

I'm not sure if which of these you need but if you force their background to be from the parent then it should resolve the issue.
XML
<border x:name="ButtonBorder" xmlns:x="#unknown">
                              CornerRadius="10,10,10,10" 
                              BorderThickness="4,4,4,4" 
                              removed="#00000000"
                              BorderBrush=" #FF8F8F8F" 
                              RenderTransformOrigin="0.5,0.5"
Background="{TemplateBinding Background}"
                    ></border>

or
XML
                  <contentpresenter x:name="ButtonContentPresenter" xmlns:x="#unknown">
                             VerticalAlignment="Bottom"  
                             HorizontalAlignment="Center"
Background="{TemplateBinding Background}"
                    />  </contentpresenter>


You shouldn't need to change the button XAML, just the control template.
 
Share this answer
 
Comments
Penc 30-Apr-13 11:20am    
Thank you to both!
I use the first solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900