cce1911
cce1911

Reputation: 353

User Control with Viewbox - binding Path Data

I have a usercontrol that contains the following Xaml:

<Viewbox Style="{StaticResource shapeViewboxStyle}">
        <Canvas Width="32" Height="32" Margin="5">
            <Path Data="{Binding Path=PathData}"/>
        </Canvas>
 </Viewbox>

In the code-behind I expose a dependency property:

    public string PathData
    {
        get { return (string)GetValue(PathDataProperty); }
        set { SetValue(PathDataProperty, value); }
    }

    public static readonly DependencyProperty PathDataProperty =
        DependencyProperty.Register("PathData", typeof(string), 
            typeof(ContentItemHeader), new PropertyMetadata(null));

What I want to do is pass in a string with the path data when I insert the user control.

PathData="M 0,0 V 32 H 32 V 0 H 0 M 0,8 H 32"

But this doesn't work. I get no image at all. What am I doing wrong?

Upvotes: 0

Views: 1010

Answers (1)

Clemens
Clemens

Reputation: 128097

Set the Binding's RelativeSource to the UserControl instance:

<Path Data="{Binding PathData, RelativeSource={RelativeSource AncestorType=UserControl}}"
      ... />

Upvotes: 1

Related Questions