C#C
C#16mo ago
Rodonies

WPF Framework Canvas Automatic Scaling based on largest coordinate in list

Hi.
First of all, thank you for reading!

I have a MVVM WPF application and having some trouble with the Canvas and how to draw shapes that are automatically scaled to the canvas size.

Let's say I have a model Y that contains a list of X. X has a location and my goal is to display all X's in a fixed size canvas using circles.
The location of each X can vary wildly so for every list of X that gets selected I need to use the largest coordinate of the furthest X in that list to scale it so all the X's show up properly on the canvas, the furthest one should be located 10% away from the canvas' border. Assuming (0, 0) is the middle of my canvas the calculation for this should be
coordinate/(FurthestCoordinate/(0.9*CanvasSize/2))


I am using the CalcBinding nuget so I can do calculations in my binding, like so:
{c:Binding (Location.X*6)+500}


The problem: I can't seem to use the LargestCoordinate property from the ancestor (which is Y) in my calculation for each X
I can get that property in xaml correctly using:
{Binding Path=DataContext.LargestCoordinate, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Border}, Mode=OneWay}


but cannot use this method in my full calculation:
X="{c:Binding '(Location.X/({Binding Path=DataContext.LargestCoordinate, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Border}, Mode=OneWay}/(0.45*494))'}"

it just gives a whole lot of XAML binding errors like this one:
System.Windows.Data Warning: 40 : BindingExpression path error: 'FindAncestor' property not found on 'object' ''Planet' (HashCode=19050756)'. BindingExpression:Path=FindAncestor; DataItem='Planet' (HashCode=19050756); target element is 'TranslateTransform' (HashCode=48896832); target property is 'X' (type 'Double')

Is there any way I can use the LargestCoordinate property from Y in my calculation for each X so I can scale the locations properly?
Was this page helpful?