带 Viewbox 的用户控件 - 绑定路径数据

User Control with Viewbox - binding Path Data

我有一个包含以下内容的用户控件 Xaml:

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

在代码隐藏中,我公开了一个依赖关系 属性:

    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));

我想做的是在插入用户控件时传入一个带有路径数据的字符串。

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

但这行不通。我根本没有图像。我做错了什么?

将 Binding 的 RelativeSource 设置为 UserControl 实例:

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