Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I'd like to reference a Path as a resource and display it several times in a control but there's something I don't understand. Here is my XAML code:

XML
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid Width="20" Height="20">
  <Grid.Resources>
    <!-- I declare a path as a reusable resource -->
    <Path x:Key="myPath"
          Fill="Blue">
      <Path.Data>
        <GeometryGroup>
          <RectangleGeometry Rect="0,0,10,10" />
          <RectangleGeometry Rect="2,2,6,6" />
        </GeometryGroup>
      </Path.Data>
    </Path>
    <!-- exactly the same resource: just the key changes -->
    <Path x:Key="myPath2"
          Fill="Blue">
      <Path.Data>
        <GeometryGroup>
          <RectangleGeometry Rect="0,0,10,10" />
          <RectangleGeometry Rect="2,2,6,6" />
        </GeometryGroup>
      </Path.Data>
    </Path>
  </Grid.Resources>
  <!-- I would like to reference myPath two times -->
  <ContentControl HorizontalAlignment="Center"
                  VerticalAlignment="Center"
                  Content="{StaticResource myPath}" />
  <!-- if I write myPath instead of myPath2, the first path is not drawn -->
  <ContentControl HorizontalAlignment="Left"
                  VerticalAlignment="Bottom"
                  Content="{StaticResource myPath2}" />
  </Grid>
</Page>


Of course, I would like to get rid of the myPath2...
I suppose this happens because myPath is instanciated only once, but I don't know how to create a second instance in XAML.

Any idea?
Posted
Updated 13-Nov-12 2:09am
v2

1 solution

OK I found by myself. Instead of declaring a Path object as a resource, I declared a Style for my paths. Then I created 2 paths and applied the style on them.

For those who might be interested, here is the code:
XML
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Width="20" Height="20">
  <Grid.Resources>
    <!-- the style to be shared -->
    <Style x:Key="myPathStyle" TargetType="Path">
      <Setter Property="Fill" Value="Blue" />
      <Setter Property="Data">
        <Setter.Value>
          <GeometryGroup>
            <RectangleGeometry Rect="0,0,10,10" />
            <RectangleGeometry Rect="2,2,6,6" />
          </GeometryGroup>
        </Setter.Value>
      </Setter>
    </Style>
  </Grid.Resources>
  <!-- the first path -->
  <Path HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Style="{StaticResource myPathStyle}" />
  <!-- the second path -->
  <Path HorizontalAlignment="Left"
        VerticalAlignment="Bottom"
        Style="{StaticResource myPathStyle}" />
  </Grid>
</Page>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900