c# - 通过 XAML 将图像设置为按钮背景

标签 c# .net wpf xaml mvvm

我正在尝试通过 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/

相关文章:

c# - 如何根据单元格值更改 DataGrid 单元格背景颜色

c# - "Enable Debugging of Unmanaged Code"在哪里可以在系统运行时编辑代码?

c# - 学习Windows生态系统的最佳方法是什么?

c# - ASP.NET 3.5 网络服务

c# - ASP Web API 在不同的应用程序实例之间共享代码库?

c# - 未调用 asp.net mvc 编辑器模板并显示文本

c# - 透明背景显示黑色

c++ - 高效删除文件 Windows C++

c# - 如何从 Azure Key Vault KeyBundle 创建 X509Certificate2 对象

c# - WPF CreateBitmapSourceFromHBitmap() 内存泄漏