Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF drawing
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:
 
<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 12-Nov-12 23:58pm
Edited 13-Nov-12 2:09am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
<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>
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100