2012-08-13 207 views
1

只有一个元素可以同时使用一个资源项目吗? 例如:WPF一个元素的一个资源?

[XAML]

<Window.Resources> 
    <Image x:Key="DockImage" Source="ico/pin.gif"/> 
    <Image x:Key="UndockImage" Source="ico/pinHorizontal.gif"/> 
</Window.Resources> 

和按钮:

<Button Width="26" Name="solutionButton" Click="eventname"> 
     <DynamicResource ResourceKey="DockImage"/> 
</Button> 
<Button Width="26" Name="soundButton" Click="eventname2"> 
     <DynamicResource ResourceKey="DockImage"/> 
</Button> 

他们的图像变为UndockImage上运行时,但图片被显示,只在这些按钮中的一个。 我可以乘以DockImage和UndockImage的图像键,但我认为这会占用更多的内存2倍。一个资源键是否可以用于一个对象(同时)?

回答

1

来自UIElement的任何内容只能在一个地方显示。您不应直接定义Image资源,这是用于显示图像的控件。相反,使用一些ImageSource派生的类,如BitmapImage,它表示内存中的图像。

<Window.Resources> 
    <BitmapImage x:Key="DockImage" UriSource="ico/pin.gif"/> 
    <BitmapImage x:Key="UndockImage" UriSource="ico/pinHorizontal.gif"/> 
</Window.Resources> 

<Button Width="26" Name="solutionButton" Click="eventname"> 
    <Image Source="{StaticResource DockImage}"/> 
</Button> 
<Button Width="26" Name="soundButton" Click="eventname2"> 
    <Image Source="{StaticResource DockImage}"/> 
</Button>