通过XAML将图像设置为按钮背景
我正在尝试通过 XAML 将图像设置为按钮的背景。我有一个值转换器,可以转换图标路径并返回位图图像
public static BitmapImage GetImage(String name)
{
//return new BitmapImage from the location of the Path
}
我尝试直接将图像对象设置为按钮的内容,但这不起作用。然后我尝试在 Button.BackGround 中设置 ImageBrush 标签的来源,但这仍然不起作用。
<Button Margin="3" HorizontalAlignment="Stretch" Grid.Row="1" Grid.Column="0">
<Button.Background>
<ImageBrush ImageSource="{Binding Path=IconPath, Converter={StaticResource ImageSourceConverter}}" />
</Button.Background>
</Button>
这并不是转换器的具体问题,因为相同的逻辑可以在我的树视图节点中显示图像。我在这里做错了什么?
I am trying to set a image as a background of a button through XAML. I have a value converter that converts the icon path and returns a Bitmap Image
public static BitmapImage GetImage(String name)
{
//return new BitmapImage from the location of the Path
}
I tried directly setting the Image object as a Content of the Button but that didnt work. Then I tried setting the Source Of the ImageBrush Tag in the Button.BackGround but that still doesnt work.
<Button Margin="3" HorizontalAlignment="Stretch" Grid.Row="1" Grid.Column="0">
<Button.Background>
<ImageBrush ImageSource="{Binding Path=IconPath, Converter={StaticResource ImageSourceConverter}}" />
</Button.Background>
</Button>
Its not specifically the problem with the Converter since the Same logic works to display the images in my treeview nodes. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我没理解错的话,最初您希望将按钮的内容绑定到图标:
编辑:
添加了数据绑定的调试。
If I unterstood you right, initially you would like to bind the content of the button to an icon:
Edit:
Added debug for DataBinding.
我不确定转换器在做什么。只需将
IconPath
绑定到ImageBrush.ImageSource
就可以立即工作,无需转换器。如果它不起作用,那么尝试在按钮的内容中添加一些面板,并将面板的背景设置为 ImageBrush。像这样...
I am not sure what the converter is doing. Simply binding
IconPath
to theImageBrush.ImageSource
should work straightaway without converter.If it doesnt work then try to do have some panel inside the Button's Content and set Panel's background as ImageBrush. Like this...
很难说,我怀疑绑定失败,您的 DataContext 可能是错误的,在转换器中设置一个断点并查看传入的值,除此之外请参见 这篇文章调试提供了更多建议。
Hard to say, i would suspect the binding just fails, your DataContext may be wrong, set a breakpoint in the converter and see what value gets passed in, other than that see this article on debugging which offers some more advice.
我不确定,但我也许可以提供一些有帮助的信息。
最近,我尝试做一些更简单的事情:更改按钮(实际上是 ToggleButton)的背景颜色。我发现在代码中设置 ToggleButton 的背景属性并没有达到我的预期。这让我很惊讶。四处搜索,你会看到“如何设置切换按钮的背景?”这是一个常见问题,似乎没有简单的答案。我明白了为什么它如此复杂。
ToggleButton 有一个 ControlTemplate,它提供操作中事物的视觉外观。在默认的 ControlTemplate 中,为各种状态转换提供了动画。当按钮被按下时,WPF 会执行一个动画(我相信这适用于 WPF4、Silverlight,不确定以前的版本)。当您将鼠标悬停时,会出现另一个动画。等等。 按钮还有一个 ControlTemplate。有一种叫做 VisualStateManager 的东西可以帮助管理所有这些动画。
每个动画中使用了多种颜色,所有颜色都硬编码到默认(隐式)模板中,并且不通过命名属性公开。如果不打开 ControlTemplate 的盖子,则无法更改这些内容。背景属性在所有这些动画的外观中只扮演次要角色。换句话说,后台并没有按照您的预期进行操作。
您可能会遇到同样的事情。我不是 WPF 专家,这就是我进行对冲的原因。
获得所需内容的可能方法:
设置属性
BorderBrush
,而不是设置背景。这是用来画画的画笔按钮,我认为是在默认的 ControlTemplate 中。
创建您自己的控件,更简单,没有 ControlTemplate 中的动画,并允许背景按照您的意愿运行。
创建修改后的 ControlTemplate,并将其作为样式应用到页面中的按钮。 这里有介绍。
I don't know for certain, but I may be able to provide some information that will help.
Recently I tried to do something even simpler: change the background color on a button - actually a ToggleButton. I found that setting a ToggleButton's Background property in code did not do what I expected. This surprised me. Search around, and you'll see "how do I Set the background of a ToggleButton?" is a common question, and it seems there is no simple answer. I've learned why it is so complicated.
The ToggleButton has a ControlTemplate that provides the visual appearance of the thing in operation. Inside the default ControlTemplate, there are animations provided for various state transitions. When the button gets pressed, there's an animation that WPF performs (I believe this is for WPF4, Silverlight, and not sure about prior versions). When you hover, there's another animation. And so on. Button also has a ControlTemplate. There's something called a VisualStateManager that aids in managing all those animations.
There are numerous colors used in each animation, all hardcoded into the default (implicit) template, and not exposed by named properties. You cannot change those without opening the hood of the ControlTemplate. The Background property plays only a minor role in the appearance of all these animations. In other words, the Background does not do what you might expect.
You may be running into the same thing. I'm not a WPF expert which is why I am hedging.
Possible ways to get what you want:
instead of setting Background, set the property
BorderBrush
. This is a brush used to paint theButton, in the default ControlTemplate, I think.
create your own control, simpler, without the animations in the ControlTemplate, and allow Background to behave as you wish.
create a modified ControlTemplate, and apply it as a style to the button in your page. Here's an introduction.