1

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>
Random
  • 1,896
  • 3
  • 21
  • 33

1 Answers1

0

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>
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • What if you want to use it as LargeImageSource for the RibbonButton? – Martin Andersen May 11 '14 at 15:12
  • As you can see from the [RibbonButton.LargeImageSource Property](http://msdn.microsoft.com/en-us/library/system.windows.controls.ribbon.ribbonbutton.largeimagesource(v=vs.110).aspx) page on MSDN, this property is of type `ImageSource`. As this has no relation with a `Path` object, you can't directly set it as the `LargeImageSource` value. You would need to create an image from it first. – Sheridan May 12 '14 at 08:29
  • You can find out how to create an image from a `Visual` from my answer to the [System.Drawing.Image from ImageSource in Resources](http://stackoverflow.com/questions/21397819/system-drawing-image-from-imagesource-in-resources/21402248#21402248) question here on Stack Overflow. – Sheridan May 12 '14 at 08:30