How to render DrawingBrush elements in ResourceDictionary in WPF?

1.9k Views Asked by At

I have a Xaml like the shown below. I have generated the Xaml from an emf file in expression. I need to enumerate and render the DrawingBrush elements. How can I accomplish that? Thanks!

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DrawingBrush x:Key="UnnamedObject0" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Geometry="F1 M 269.454,99.1347L 350.683,180.322L 395.356,135.671L 314.128,54.4843L 269.454,99.1347 Z ">
                    <GeometryDrawing.Pen>
                        <Pen Thickness="0.97367" StartLineCap="Round" EndLineCap="Round" LineJoin="Round" Brush="#FF000000"/>
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

<DrawingBrush x:Key="UnnamedObject1" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="#FF000000" Geometry="F1 M 330.694,118.07L 330.694,118.008L 330.96,117.569L 331.351,117.19C 331.367,117.15 331.375,117.104 331.375,117.052C 331.375,116.961 331.35,116.891 331.3,116.841C 331.251,116.791 331.181,116.766 331.092,116.766C 331.031,116.766 330.973,116.78 330.917,116.807C 330.861,116.833 330.806,116.871 330.752,116.919L 330.752,116.816C 330.804,116.777 330.861,116.745 330.921,116.723C 330.981,116.7 331.044,116.688 331.11,116.688C 331.165,116.688 331.214,116.697 331.259,116.713L 331.373,116.783L 331.446,116.893C 331.463,116.936 331.472,116.984 331.472,117.037C 331.472,117.1 331.463,117.156 331.444,117.204C 331.426,117.252 331.401,117.296 331.37,117.336L 331.261,117.445C 331.219,117.478 331.175,117.511 331.128,117.544C 331.066,117.588 331.014,117.626 330.971,117.66L 330.809,117.859L 330.791,117.978L 330.791,117.992L 331.472,117.992L 331.472,118.07L 330.694,118.07 Z "/>
                <GeometryDrawing Brush="#FF000000" Geometry="F1 M 332.114,116.688L 332.114,118.07L 332.017,118.07L 332.017,116.828C 331.973,116.87 331.924,116.907 331.868,116.938C 331.812,116.968 331.752,116.996 331.686,117.019L 332.083,116.688L 332.114,116.688 Z "/>
                <GeometryDrawing Brush="#FF000000" Geometry="F1 M 332.425,118.018L 332.425,117.92C 332.529,117.981 332.626,118.011 332.718,118.011C 332.827,118.011 332.912,117.984 332.974,117.928C 333.036,117.873 333.067,117.801 333.067,117.711C 333.067,117.612 333.032,117.536 332.962,117.485C 332.892,117.434 332.793,117.408 332.664,117.408L 332.561,117.408L 332.561,117.331L 332.651,117.331C 332.767,117.331 332.856,117.304 332.917,117.252C 332.978,117.2 333.009,117.126 333.009,117.031C 333.009,116.949 332.986,116.884 332.941,116.837C 332.896,116.79 332.831,116.766 332.745,116.766C 332.661,116.766 332.574,116.797 332.483,116.858L 332.483,116.765C 332.584,116.714 332.677,116.688 332.761,116.688C 332.867,116.688 332.951,116.718 333.013,116.776C 333.075,116.835 333.106,116.912 333.106,117.009C 333.106,117.194 333.022,117.312 332.853,117.362L 332.853,117.366C 332.947,117.375 333.023,117.41 333.079,117.47C 333.136,117.53 333.164,117.606 333.164,117.698C 333.164,117.815 333.123,117.91 333.04,117.982C 332.957,118.053 332.85,118.089 332.718,118.089C 332.616,118.089 332.519,118.065 332.425,118.018 Z "/>
                <GeometryDrawing Brush="#FF000000" Geometry="F1 M 333.495,118.054L 333.495,117.957C 333.568,117.993 333.635,118.011 333.695,118.011C 333.839,118.011 333.949,117.954 334.024,117.838C 334.099,117.723 334.137,117.553 334.137,117.328L 334.133,117.328C 334.07,117.472 333.958,117.544 333.796,117.544C 333.74,117.544 333.688,117.534 333.642,117.513L 333.522,117.428C 333.488,117.391 333.463,117.348 333.444,117.298C 333.426,117.247 333.417,117.192 333.417,117.132C 333.417,117.066 333.428,117.005 333.449,116.951C 333.469,116.897 333.498,116.85 333.536,116.811C 333.573,116.773 333.617,116.742 333.668,116.721C 333.719,116.699 333.775,116.688 333.835,116.688C 333.896,116.688 333.951,116.701 334,116.727C 334.05,116.752 334.092,116.791 334.126,116.842C 334.161,116.893 334.188,116.957 334.206,117.034C 334.225,117.11 334.234,117.199 334.234,117.302C 334.234,117.427 334.223,117.539 334.199,117.637C 334.176,117.734 334.142,117.817 334.096,117.884C 334.051,117.951 333.995,118.002 333.928,118.037C 333.861,118.072 333.783,118.089 333.695,118.089C 333.627,118.089 333.56,118.077 333.495,118.054 Z M 333.818,116.766L 333.6,116.865L 333.537,116.976C 333.522,117.019 333.514,117.065 333.514,117.114C 333.514,117.169 333.522,117.218 333.536,117.261L 333.693,117.442L 334.118,117.159C 334.118,117.102 334.11,117.05 334.095,117.001L 333.939,116.795L 333.818,116.766 Z "/>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>


</ResourceDictionary>
1

There are 1 best solutions below

5
On

You could use an ItemsControl and bind a list of the DrawingBrush from the ResourceDictionary

Example:

Xaml:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Name="UI" Width="262" Height="318">

    <Grid DataContext="{Binding ElementName=UI}">
        <ItemsControl ItemsSource="{Binding Visuals}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Rectangle Width="200" Height="200" Fill="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

Code:

namespace WpfApplication8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // Application Resources or any other ResourceDictionary
        public IEnumerable<DrawingBrush> Visuals
        {
            get { return Application.Current.Resources.Values.OfType<DrawingBrush>(); }
        }
    }
}

Result: enter image description here


Edit:

If you are using MergedDictionaries you can use

public IEnumerable<DrawingBrush> Visuals
{
    get { return Application.Current.Resources.MergedDictionaries.SelectMany(r => r.Values.OfType<DrawingBrush>()); }
}