xaml如何将动态资源引用为属性而不是元素

use*_*331 3 silverlight wpf xaml

我有一个图像,我需要在我的应用程序中使用几个地方.我想在资源字典中只定义一次图像,并让我的其他xaml文件就是那个定义.我能做的一件事我无法弄清楚的是如何引用定义为xaml元素的东西而不是xaml属性中的属性.

这是我的ResourceDictionary

# resourceDictionary.xaml

<LinearGradientBrush x:Key="MyGradient" StartPoint="0,0.5" EndPoint="1,0.5">
    <GradientStop Color="#A5000000" Offset="0"/>
    <GradientStop Color="#00000000" Offset="1"/>
</LinearGradientBrush>

<Image x:Key="MyImage" Source="MyGlyph.png" Width="20" Height="20" />
Run Code Online (Sandbox Code Playgroud)

所以在我的xaml中我知道如何引用渐变作为控件对象的属性

<TextBlock Text="Sample Text" Background="{DynamicResource MessageGradient}"/>
Run Code Online (Sandbox Code Playgroud)

但我想弄清楚他如何引用Image是一个完整的控制对象.此示例仅创建一个按钮,该按钮在按钮中具有文本"{DynamicResource MyImage}"而不是图像.

<!-- Want MyImage to be content of the button -->
<Button>
   {DynamicResource MyImage}
</Button>
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法可以做到这一点,或者我必须创建一个仅包含我的图像的控件模板,然后在我的xaml中有一个使用控件模板的图像标签?

And*_*ndy 5

由于您希望图像是按钮的内容,因此您应该能够将其绑定到Content属性:

<Button Content="{DynamicResource MyImage}" />
Run Code Online (Sandbox Code Playgroud)