造型一个WPF按钮与图片+文字 [英] Styling a WPF Button with Image+Text
问题描述
在我的C#/ WPF / .NET 4.5的应用程序我有,我在下面的方式实现图像按钮:
In my C#/WPF/.NET 4.5 application I have buttons with images that I implemented in the following fashion:
<Button Style="{StaticResource BigButton}">
<StackPanel>
<Image Source="Images/Buttons/bt_big_save.png" />
<TextBlock>save</TextBlock>
</StackPanel>
</Button>
我有资源字典中的 UIStyles.xaml 中,我声明如下
<Style TargetType="Button" x:Key="BigButton">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Height" Value="80" />
<Setter Property="Width" Value="80" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border"
CornerRadius="5"
Background="#FF415360">
<ContentPresenter x:Name="ButtonContentPresenter"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<ContentPresenter.Resources>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
</Style>
<Style TargetType="Image">
<Setter Property="Width" Value="10" />
<Setter Property="Margin" Value="10" />
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
光标,身高,边框等性能做工精细,但我不能样式的的TextBlock
和图片
。
具体来讲,需要寻找像这样的:
Specifically, what needs to look like this:
最终看起来像这样(不考虑色差):
Ends up looking like this (disregarding the color difference):
我见过的类似的问题问过,但解决方案中使用不同的方法(我不希望创建一个自定义用户控件,我所有的需求除了这一个覆盖在目前的代码,并重新书写将是一个滋扰)。我只是需要修复我的风格
,使的TextBlock
为中心,并在图片
为中心,并变得更小。
I've seen similar questions asked before but the solutions used different approaches (I don't want to create a custom User Control, all of my needs except this one are covered in the present code and re-writing will be a nuisance). I merely need to fix my Style
so that the TextBlock
is centered and the Image
is centered and made smaller.
我怎样重新写入风格
来纠正我的按钮的外观?
How do I re-write the Style
to correct the look of my buttons?
推荐答案
你的问题的解决方案可能是你的形象和TextBlock的风格移到控件模板的资源部分。我不知道的为什么的,但我相信风格不在范围内,如果他们是内容的演示者的资源的一部分。
The solution to your problem may be to move your Image and TextBlock styles to the ControlTemplate's Resource section. I'm not sure why, but I believe the styles aren't in scope if they are part of the content presenter's resources.
这篇关于造型一个WPF按钮与图片+文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!