创建带有图像和文本的 wpf 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15164907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:42:08  来源:igfitidea点击:

Create wpf button with an image and a text

wpfxamltemplatesstyles

提问by Walter Fabio Simoni

I would like to create a button template in order to display an image and a label in the button. I thiking about use an horizontal stackpanel on the button.

我想创建一个按钮模板,以便在按钮中显示图像和标签。我想在按钮上使用水平堆栈面板。

I don't succeed 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 of 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 Textbox instead a Label

这是另一种使用文本框而不是标签的方法

    <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 is i see onl my image, and not my label specified in the template. Anyone could help me please ?

我的问题是我只能看到我的图像,而不是我在模板中指定的标签。任何人都可以帮助我吗?

Thanks a lot :)

非常感谢 :)

回答by Nathan Hillyer

A TextBlock fits your needs better than a label. If you don't use the extra label functionality (such as target, object content), stick with a good 'ole TextBlock.

TextBlock 比标签更适合您的需求。如果您不使用额外的标签功能(例如目标、对象内容),请坚持使用一个好的 'ole TextBlock。

If you want to disable all button styling and only have an image and label in the button (note, this nullifies mouseover/mousedown effects, but you can provide the logic in triggers):

如果您想禁用所有按钮样式并且按钮中只有一个图像和标签(注意,这会使鼠标悬停/鼠标按下效果无效,但您可以在触发器中提供逻辑):

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

Usage:

用法:

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

If you just want the Image/TextBlock in a normal styled button, it is even easier:

如果您只想要普通样式按钮中的图像/文本块,那就更容易了:

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

回答by Ramesh

Use canvas as follows.

如下使用画布。

<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>

You can adjust Canvas.Leftand Canvas.Topas per your requirements. I hope this one will help you as it will display Textblock on top of the image.

您可以调整Canvas.Left,并Canvas.Top根据您的要求。我希望这个可以帮助你,因为它会在图像顶部显示 Textblock。