wpf 中带有图像和文本的按钮模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2726986/
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
Button template with image and text in wpf
提问by Archie
I want to create buttons with images and text inside. For example, i would use different images and text for buttons like 'Browse folders' and 'Import'. One of the options would be to use a template. I had a look at simliar question
我想创建带有图像和文本的按钮。例如,我会为“浏览文件夹”和“导入”等按钮使用不同的图像和文本。选项之一是使用模板。我看过类似的问题
Creating an image+text button with a control template?
But is there any way by which i can bind the source of image without using a dependency property or any other class?
但是有什么方法可以在不使用依赖属性或任何其他类的情况下绑定图像源?
Thanks
谢谢
采纳答案by gehho
No. What would you bind the Image.Source
to? You need a DependencyProperty for this. Of course, you could also define a normal class which contains two properties: Text
and ImageSource
or Uri
, and then use a DataTemplate to render instances of this class, but that would be even more code to write, and it is a little "smelly".
不。你会绑定什么Image.Source
?为此,您需要一个 DependencyProperty。当然,你也可以定义一个普通的类,它包含两个属性:Text
andImageSource
或Uri
,然后使用 DataTemplate 来渲染这个类的实例,但这会写更多的代码,而且有点“臭”。
What is the reason you do not want to use a dependency property or a custom class?
您不想使用依赖属性或自定义类的原因是什么?
回答by Marshal
It doesn't have to be that complicated. Something as simple as putting a StackPanel inside a button will do the trick:
它不必那么复杂。像将 StackPanel 放在按钮中这样简单的事情就可以解决问题:
<Button>
<StackPanel>
<TextBlock>My text here</TextBlock>
<Image Source="some.jpg" Stretch="None" />
</StackPanel>
</Button>
Then you can configure the StackPanel to control where the text should appear, alignment, etc.
然后你可以配置 StackPanel 来控制文本应该出现的位置、对齐方式等。
回答by Sean
I added a few things to line them up nicely
我添加了一些东西来很好地排列它们
<Button>
<StackPanel Orientation="Horizontal">
<Image Source="/ApplicationName;component/Images/MyImage.ico"/>
<Label Padding="0">My Button Text</Label>
</StackPanel>
</Button>
回答by mpen
<Button>
<StackPanel Orientation="Horizontal">
<Image Source="Resources/add.png" Stretch="None" />
<TextBlock Margin="5,0,0,0">Add</TextBlock>
</StackPanel>
</Button>
回答by daniel
<Button x:Name="MyCoolButton"Width="200" Height="75">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="Pete-Brown-Silverlight-in-Action.png" Margin="5" Grid.Column="0" />
<StackPanel Grid.Column="1" Margin="5">
<TextBlock Text="Buy My Book!" FontWeight="Bold" />
<TextBlock Text="Pete is writing THE Silverlight 4 book" TextWrapping="Wrap" />
</StackPanel>
</Grid>
回答by userN45290
Added Stretch="Uniform"to Sean's answer to address case if the image is originally larger than the button size (issue BrainSlugs83 mentioned in his comments that I ran into as well). More details of Stretch options at MSDN.
如果图像最初大于按钮大小,则在 Sean 的回答中添加了Stretch="Uniform"以解决案例(我也在他的评论中提到的问题 BrainSlugs83)。MSDN 上有关 Stretch 选项的更多详细信息。
<Button>
<StackPanel Orientation="Horizontal">
<Image Source="/ApplicationName;component/Images/MyImage.ico" Stretch="Uniform"/>
<Label Padding="0">My Button Text</Label>
</StackPanel>
</Button>
Wanted to add this as a comment to the answer under BrainSlugs83 but can't yet due to lack of points and was rejected from editing Sean's answer.
想将此作为评论添加到 BrainSlugs83 下的答案中,但由于缺乏积分而无法添加,并且被拒绝编辑 Sean 的答案。
回答by marsh-wiggle
For me the IconButtonof the XCeed WPF Toolkit (freeware) does the trick.
对我来说,XCeed WPF Toolkit(免费软件)的 IconButton 可以解决问题。