绑定UserControl上的Path.Data属性

时间:2016-04-26 15:31:41

标签: c# wpf binding controls

我希望与2011年的热门问题相同: Should binding to Path.Data property work?但我的问题是我有任何其他错误,我不理解女巫。我继续寻找自从我看错了什么,但我必须是盲目的......

Generic.xaml:

<Style TargetType="{x:Type local:CustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Path Data="{Binding GeometryData}" x:Name="CurveGraph" Stroke = "Black" StrokeThickness = "2" Grid.RowSpan="4"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

CustomControl.cs:

public class CustomControl : Control    
{

static CustomControl()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
    CustomControl tmp = new CustomControl();
    tmp.Build();
}

public GeometryGroup Build()
{
    var curve = new GeometryGroup();
    curve.Children.Add(new LineGeometry(new Point(0, 0), new Point(20, 20)));
    curve.Children.Add(new LineGeometry(new Point(0, 20), new Point(20, 0)));
    new CustomControl().GeometryData = curve;
    return curve;
}

private GeometryGroup GeometryData
{
    get { return (GeometryGroup)GetValue(GeometryDataProperty); }
    set { SetValue(GeometryDataProperty, value); }
    public static readonly DependencyProperty GeometryDataProperty = DependencyProperty.Register("GeometryData", typeof(GeometryGroup), typeof(CustomControl), new UIPropertyMetadata(new GeometryGroup()));
}

MainWindow.xaml:

<Grid>
    <ctl:CustomControl x:Name="Curve"/>
</Grid>

就是这样。主Window.xaml.cs只有他的构造函数。但我真的不知道问题是什么。 :(

1 个答案:

答案 0 :(得分:1)

在(静态)类构造函数中创建临时CustomControl实例并调用Build方法是没有意义的,该方法在另一个临时实例上设置GeometryData

而是添加一个在当前实例上初始化GeometryData的实例构造函数。同时公开GeometryData

public class CustomControl : Control
{
    public static readonly DependencyProperty GeometryDataProperty =
        DependencyProperty.Register(
            "GeometryData", typeof(GeometryGroup), typeof(CustomControl));

    public GeometryGroup GeometryData
    {
        get { return (GeometryGroup)GetValue(GeometryDataProperty); }
        set { SetValue(GeometryDataProperty, value); }
    }

    static CustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl),
            new FrameworkPropertyMetadata(typeof(CustomControl)));
    }

    public CustomControl()
    {
        Build();
    }

    public void Build()
    {
        var curve = new GeometryGroup();
        curve.Children.Add(new LineGeometry(new Point(0, 0), new Point(20, 20)));
        curve.Children.Add(new LineGeometry(new Point(0, 20), new Point(20, 0)));
        GeometryData = curve;
    }
}

除此之外,你可以在ControlTemplate中使用TemplateBinding

<Path Data="{TemplateBinding GeometryData}" ... />

或指定绑定源,如

<Path Data="{Binding GeometryData, RelativeSource={RelativeSource TemplatedParent}}" ... />