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

gan*_*ran 5 .net c# wpf xaml mvvm

我试图通过XAML将图像设置为按钮的背景.我有一个转换图标路径的值转换器并返回一个位图图像

public static BitmapImage GetImage(String name)
{ 
      //return new BitmapImage from the location of the Path
}
Run Code Online (Sandbox Code Playgroud)

我尝试直接将Image对象设置为Button的内容但是没有用.然后我尝试在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>
Run Code Online (Sandbox Code Playgroud)

它并不特别是转换器的问题,因为Same逻辑用于在我的treeview节点中显示图像.我在这做错了什么?

Wal*_*tiD 6

如果我理解你的话,最初你想将按钮的内容绑定到一个图标:

<Button Grid.Column="1">
        <Button.Content>
            <Image Source="{Binding IconPath, PresentationTraceSources.TraceLevel=High}" />
        </Button.Content>
</Button>
Run Code Online (Sandbox Code Playgroud)

编辑:
添加了DataBinding的调试.