I'm trying to bind two points of a line to two Ellipses, which are inside ScatterViewItems. At the moment, I'm able to bind to ActualCenter.X and .Y for the ScatterViewItem, which works, but because the ellipse is not the only element in that ScatterViewItem (there is also a label), it's off-center, such that the line's endpoints are not the center of the ellipse.
Ellipse doesn't have an ActualCenter property.
I wanted to know whether I'm able to offset a property binding by a fixed amount, or whether there is some other kind of binding I can use such that I can work out the correct ellipse center in the container class (for the ellipse, label and scatterviewitem), and return that as the source of the binding.
Below is the code where I am setting the binding. this.avatar
refers to a Line
shape. node_one
and node_two
are the container objects, which contain a ScatterViewItem
. The Node
class also contains the Ellipse
, the centre for which I would essentially like as the source, but I'm happy with a simple offset of the current source.
BindingOperations.SetBinding(this.avatar, Line.X1Property, new Binding
{
Source = node_one.getScatterViewItem(),
Path = new PropertyPath("ActualCenter.X")
});
BindingOperations.SetBinding(this.avatar, Line.Y1Property, new Binding
{
Source = node_one.GetNodeCenterY(),
Path = new PropertyPath("GetNodeCenterY()")
});
// Bind line.(X2,Y2) to destination.ActualCenter
BindingOperations.SetBinding(this.avatar, Line.X2Property, new Binding
{
Source = node_two.getScatterViewItem(),
Path = new PropertyPath("ActualCenter.X")
});
BindingOperations.SetBinding(this.avatar, Line.Y2Property, new Binding
{
Source = node_two.GetNodeCenterY(),
Path = new PropertyPath("GetNodeCenterY()")
});
I attempted to set to the Source
as node_one
and Path
as new PropertyPath("getOffsetCenter()")
which returns a double, but it didn't work. It didn't complain, but it didn't work :) Any tips are appreciated :)
Edit, trying to use IValueConverter:
[ValueConversion(typeof(Double), typeof(Double))]
public class OffsetValues : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
Double y = (Double)value;
Double offset = (Double)parameter;
return y + offset;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Double dblValue = (Double)value;
Double offset = (Double)parameter;
Double resultDblValue = dblValue - offset;
return resultDblValue;
}
}
The above was added as a new class, but how would I attach it to the source of my binding - the example at MSDN has a XAML based implementation but mine is all programmatic.