Jaa


x:Shared Attribute in xaml (1)

I have created a simple wpf application. In App.xaml, I defined two resources: one is an Image control(key is image") and another is a DataTemplate which using DynamicResource to reference the Image.

 <Application.Resources>
        <Image x:Key="image" Source="Resource\Dock.jpg"/>
        <DataTemplate x:Key="dataTemplate" x:Shared="false">
            <Border Background="Blue" Margin="10">
                <ContentControl Content="{StaticResource image}"/>
            </Border>
        </DataTemplate>
</Application.Resources>

And then in Window1.xaml file, I put 3 ContentControls to use the DataTemplate.

 <StackPanel>       
       <TextBlock Text="First"/>
       <ContentControl Height="150"  
            ContentTemplate="{DynamicResource dataTemplate}"/>
       
       <TextBlock Text="Second"/>
       <ContentControl Height="150" 
            ContentTemplate="{DynamicResource dataTemplate}"/>
       
       <TextBlock Text="Third"/>
       <ContentControl Height="150"
            ContentTemplate="{DynamicResource dataTemplate}"/>
   </StackPanel>

And then run this app, the result is that:

One-Image-Result 

You can see that there is just one image shown.

But if we set x:Shared=”false” for the Image resource, like this:

 <Image x:Key="image" x:Shared="false" Source="Resource\Dock.jpg"/>

 

And re-run the app again, this result is:

right-image

Comments