Reputation: 273
I'm completely new to programming in general, but I've been teaching myself C# for about 6 months. Currently working on WPF/XAML. I'm trying to learn about rendering shapes in WPF.
I have a Path created in a custom class that my AllocationCanvas is binding to in the XAML below. 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. 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);
}
}
}
Upvotes: 2
Views: 4296
Reputation: 81233
You are binding your Data
DP of Path
to object of type Path
which won't work.
Data
is Geometry
type so you need to bind to an object which will return Geometry
and not Path
.
Make your pathGeomerty
a property
and bind with it -
public PathGeometry Geometry
{
get
{
return pathGeometry ;
}
}
XAML -
<Path Data="{Binding Path=chartmaker.Geometry}"/>
Upvotes: 1