简体   繁体   English

在XAML(带有绑定)和C#中绘制路径。 一种有效,一种无效

[英]Drawing a Path in XAML (with binding) vs. C#. One works, one doesn't

I'm completely new to programming in general, but I've been teaching myself C# for about 6 months. 一般来说,我对编程完全陌生,但是自学C#已有6个月了。 Currently working on WPF/XAML. 当前正在使用WPF / XAML。 I'm trying to learn about rendering shapes in WPF. 我正在尝试学习在WPF中渲染形状。

I have a Path created in a custom class that my AllocationCanvas is binding to in the XAML below. 我在以下XAML中的AllocationCanvas绑定到的自定义类中创建了一个Path。 For whatever reason, it just won't draw, even though I know the binding is set up correctly. 无论出于什么原因,即使我知道绑定设置正确,它也不会绘制。

<Canvas x:Name="AllocationCanvas"
                    Grid.Row="0"
                    Grid.Column="0"
                    Width="Auto"
                    Height="Auto"
                    DataContext="{Binding RelativeSource={RelativeSource FindAncestor,
                                                                         AncestorType={x:Type Window}}}">
    <Path Data="{Binding Path=chartmaker.myPath}"/>

</Canvas>

However, if I call AllocationCanvas.Children.Add(myPath) it works fine. 但是,如果我调用AllocationCanvas.Children.Add(myPath),它可以正常工作。 What am I missing? 我想念什么?

public class ChartMaker {

    private Dictionary<string, double> chartData;
    Portfolio P;
    public PathFigure arcs { get; set; }
    private PathGeometry pathGeometry = new PathGeometry();
    public Path myPath { get; set; }

    public ChartMaker(Portfolio portfolio) {
        P = portfolio;
        chartData = new Dictionary<string, double>();

        myPath = new Path();
        myPath.Stroke = Brushes.Black;
        myPath.Fill = Brushes.MediumSlateBlue;
        myPath.StrokeThickness = 4;

        arcs = new PathFigure();
        arcs.StartPoint = new Point(100, 100);
        arcs.IsClosed = true;
        arcs.Segments.Add(new ArcSegment(new Point(200, 200), new Size(50, 50), 0, false, SweepDirection.Clockwise, true));
        arcs.Segments.Add(new ArcSegment(new Point(150, 350), new Size(10, 10), 0, false, SweepDirection.Clockwise, true));

        pathGeometry.Figures.Add(arcs);

        myPath.Data = pathGeometry;

        ProcessChartData();
    }

    private void ProcessChartData() {
        double TotalMarketValue = P.Positions.Sum(position => position.MarketValue);

        foreach (Position position in P.Positions) {
            double weight = position.MarketValue/TotalMarketValue;
            chartData.Add(position.Ticker, weight);
        }


    }
}

You are binding your Data DP of Path to object of type Path which won't work. 你要绑定您的Data的DP Path类型为对象Path ,这将无法正常工作。

Data is Geometry type so you need to bind to an object which will return Geometry and not Path . DataGeometry类型的,因此您需要绑定到将返回Geometry而不是Path的对象。

Make your pathGeomerty a property and bind with it - pathGeomertyproperty并与其绑定-

public PathGeometry Geometry
{
   get
   {
      return pathGeometry ;
   }
}

XAML - XAML-

<Path Data="{Binding Path=chartmaker.Geometry}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM