Vector image as reusable XAML fragment

I want to reuse some XAML fragment as image in some WPF application/library.

The background of the problem is following:

It's easy to reuse a bitmap image in a WPF application. The image can be added as a resource, I can use at many places in XAML so the image will be the same.

But I'd like to have a possibility to do the same for a vector image. The image itself can be represented as Path, but I cannot reuse the same Path as a resource, because simply using it at several different places (and possibly from several UI threads) is prohibited (UI element can have only one logical parent).

Moreover, the question gets more complicated if I'd like to build the "image" from several Paths, use a Canvas for it. Or some arbitrary XAML code.

I tried using a Style for the Path, so the image is represented in such a way:

<Path Style={StaticResource VectorImage1}/>

This seems to be a reusable way, but I am concerned with two problems:

  1. If the implementation of a vector image is changed from Path to a (for instance) Canvas, I'll need to replace it not only in the style, but everywhere in the source code which uses it.
  2. Definition of a path using a style seems to be too verbose.
  3. I see no way to generalize this approach for using Canvas or arbitrary XAML code.
  4. The syntax seems to be quite unnatural.

There is other way to have a reusable XAML fragment, through defining a UserControl, but defining a separate user control for each vector image seems to be an overkill.

Is there a better, nice, right way to define a reusable XAML fragment?

9
задан Vlad 8 December 2010 в 12:20
поделиться