Create a wpf button with image and text

I would like to create a button template to display the image and label in the button. I am trying to use a horizontal stack panel on a button.

I am unable to display the label.

Here is my template:

<ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonImageEtTexte">
<Button Grid.Column="2" BorderBrush="Transparent" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Width="90" Height="27" >
    <Button.Content>
        <StackPanel Orientation="Horizontal">
            <Border CornerRadius="3">
                <ContentPresenter/>
                <Border.Style>
                    <Style TargetType="{x:Type Border}">
                        <Setter Property="Background" Value="#58585a" />
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="{StaticResource DegradeCouleurTheme}" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
            </Border>
            <Label>
                <Label.Content>Fichiers joints</Label.Content>
                <Label.Foreground>white</Label.Foreground>
                <Label.VerticalAlignment>center</Label.VerticalAlignment>
                <Label.HorizontalAlignment>center</Label.HorizontalAlignment>
            </Label>
        </StackPanel>
    </Button.Content>
    <Button.Style>
        <Style TargetType="{x:Type Button}" >
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}" >
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

Here is my call to this template:

<Grid Margin ="10,180,10,14">
            <Button Template="{StaticResource BoutonImageEtTexte}" HorizontalAlignment="Left" Margin="13,0,0,0"> 
                <Image Source="ToolBar_FicJoints.png" />
            </Button>
        </Grid> 

Here is another method with a text box, not an inscription

    <ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonImageEtTexte">
    <Button Grid.Column="2" BorderBrush="Transparent" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Width="90" Height="27" >
        <Button.Content>
            <StackPanel Orientation="Horizontal">
                <Border CornerRadius="3">
                    <ContentPresenter/>
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="Background" Value="#58585a" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="{StaticResource DegradeCouleurTheme}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                </Border>
                    <TextBlock Text="LabelText" />
                </StackPanel>
        </Button.Content>
        <Button.Style>
            <Style TargetType="{x:Type Button}" >
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}" >
                            <ContentPresenter />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
    </Button>
</ControlTemplate>

My problem: I see onl my image, not my shortcut specified in the template. Can anybody help me?

Many thanks:)

+5
source share
2 answers

TextBlock fits your needs better than a label. If you do not use additional label functionality (for example, target, object content), stick to a good “text block”.

( , mouseover/mousedown, ):

<ControlTemplate TargetType="{x:Type Button}" x:Key="SimpleButton">
    <ContentPresenter/>
</ControlTemplate>

:

<Button Template="{StaticResource SimpleButton}"> 
    <StackPanel Orientation="Horizontal">
        <Image Source="ToolBar_FicJoints.png" />
        <TextBlock Text="LabelText" />
    </StackPanel>
</Button>

Image/TextBlock , :

<Button> 
    <StackPanel Orientation="Horizontal">
        <Image Source="ToolBar_FicJoints.png" />
        <TextBlock Text="Fichiers joints" />
    </StackPanel>
</Button>
+12

.

<Button Height="50" Width="150" >
                        <Button.Template>
                            <ControlTemplate>
                                <Canvas>
                                    <Image Source="./Images/Cancel.png"/>
                                    <TextBlock Canvas.Left="22" Canvas.Top="8" Text="Cancel" FontFamily="Arial" FontSize="18"/>
                                </Canvas>
                            </ControlTemplate>
                        </Button.Template>
                     </Button>

Canvas.Left Canvas.Top . , , Textblock.

+3

All Articles