Can Shapes.Path data be used as an image source in WPF?

1.7k views Asked by At

I using some vector graphics in my WPF app, that I am accessing through a ResourceDictionary. I am now creating a TreeView where I would like to replace the default images used to preface the nodes with the vector graphics. Defining different images for the nodes is a piece-o-cake if they are real images, but is there any way I can use the Path element as the image source in the TreeView?

I'd rather use the vectors than actual images because I'm using the vector graphics in other areas of the site, and it'd be preferable to just have one image source.

An example of an entry in the ResourceDictionary is as follows:

<ControlTemplate x:Key="MyPath">
   <Viewbox>
      <Grid>
         <Path Data="F1M2357.31445,2509.047846875L2456.35742,2509.047846875C2461.00781,2509.047846875,2464.78906,2504.952146875,2464.78906,2499.916996875C2464.78906,2494.881836875,2461.00781,2490.785156875,2456.35742,2490.785156875L2357.31445,2490.785156875C2352.66602,2490.785156875,2348.88477,2494.881836875,2348.88477,2499.916996875C2348.88477,2504.952146875,2352.66602,2509.047846875,2357.31445,2509.047846875z" Fill="White" Stretch="Fill" />            
      </Grid>
    </Viewbox>
 </ControlTemplate>
1

There are 1 answers

3
Sheridan On

I'm not sure what default images you're talking about, but in WPF, you can just define a DataTemplate for your items that contains any UI elements that you chose. Therefore, there is no reason to use an Image to display your vector graphics:

<TreeView.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <Path Data="F1M2357.31445,2509.047846875L2456.35742,2509.047846875C2461.00781,2509.047846875,2464.78906,2504.952146875,2464.78906,2499.916996875C2464.78906,2494.881836875,2461.00781,2490.785156875,2456.35742,2490.785156875L2357.31445,2490.785156875C2352.66602,2490.785156875,2348.88477,2494.881836875,2348.88477,2499.916996875C2348.88477,2504.952146875,2352.66602,2509.047846875,2357.31445,2509.047846875z" Fill="White" Stretch="Fill" />
            <ContentPresenter Content="{Binding}" Margin="5,0,0,0" />
        </StackPanel>
    </DataTemplate>
</TreeView.ItemTemplate>