我正在尝试通过 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>
这不是 Converter 的具体问题,因为相同的逻辑用于在我的 TreeView 节点中显示图像。我在这里做错了什么?
最佳答案
如果我没听错的话,最初您想将按钮的内容绑定(bind)到一个图标:
<Button Grid.Column="1">
<Button.Content>
<Image Source="{Binding IconPath, PresentationTraceSources.TraceLevel=High}" />
</Button.Content>
</Button>
编辑:
为 DataBinding 添加了调试。
关于c# - 通过 XAML 将图像设置为按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7347415/