问题描述
我想创建带有图像和文本的按钮.例如,我会为浏览文件夹"和导入"等按钮使用不同的图像和文本.
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 similar question
但是有没有什么方法可以在不使用依赖属性或任何其他类的情况下绑定图像源?
But is there any way by which I can bind the source of image without using a dependency property or any other class?
推荐答案
没有.你会将 Image.Source
绑定到什么?为此,您需要一个 DependencyProperty.当然,你也可以定义一个包含两个属性的普通类:Text
和 ImageSource
或 Uri
,然后使用 DataTemplate 来渲染实例这个类的,不过这样写的代码会更多,而且有点臭".
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".
您不想使用依赖属性或自定义类的原因是什么?
What is the reason you do not want to use a dependency property or a custom class?
这篇关于wpf 中带有图像和文本的按钮模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!