1

I am trying to make a pie chart custom control, and I will need to have an unknown number of pie slices. I am trying to accomplish this by creating a custom property, called maybe DrawingCode, and this code will be a string which can be converted and interpreted by a DrawingGroup. I was hoping for DrawingGroup.Children to be bound to TemplateBinding DrawingCode and use a converter, but I don't know how to convert a string into a list of GeometryDrawings. Any help is appreciated!

Here is the ResourceDictionary containing the generic for my PieChart control:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFTest">


<Style TargetType="{x:Type local:PieChart}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:PieChart}">
                <Image>
                    <Image.Source>
                        <DrawingImage>
                            <DrawingImage.Drawing>
                                <DrawingGroup Children="{TemplateBinding DrawingCode}" />
                            </DrawingImage.Drawing>
                        </DrawingImage>
                    </Image.Source>
                </Image>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Daniel G
  • 29
  • 1
  • 1
  • 9

2 Answers2

2

Your converter could return a Drawing (e.g. a DrawingGroup), which you would directly bind to the DrawingImage.Drawing property:

<DrawingImage Drawing="{Binding DrawingCode, 
    RelativeSource={RelativeSource AncestorType=TemplatedParent}
    Converter={StaticResource YourConverter}}"/>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • 1
    So if I understand correctly, I make a DrawingGroup in the converter that has a list of children which contain GeometryDrawings in them? – Daniel G Jun 23 '17 at 21:48
0

I would say - use a ListBox with a Canvas as ItemsPanel. This way you get all the nice stuff from the ListBox - from multiple items to selection.

One approach to this is answered here. Of course you still have to convert a string into a drawing, but this way you dont have to worry about having multiple items and of their positioning.

Nuri
  • 1
  • 2