我使用以下Bindings连接两个省略号:
Line l = new Line();
l.Stroke = Brushes.Green;
l.StrokeThickness = 3;
Binding x1 = new Binding(); x1.Path = new PropertyPath(Canvas.LeftProperty);
Binding y1 = new Binding(); y1.Path = new PropertyPath(Canvas.TopProperty);
Binding x2 = new Binding(); x2.Path = new PropertyPath(Canvas.LeftProperty);
Binding y2 = new Binding(); y2.Path = new PropertyPath(Canvas.TopProperty);
x1.Source = y1.Source = e;
x2.Source = y2.Source = e1;
l.SetBinding(Line.X1Property, x1);
l.SetBinding(Line.Y1Property, y1);
l.SetBinding(Line.X2Property, x2);
l.SetBinding(Line.Y2Property, y2);
Dependencies.Children.Add(l);
这很有用,但问题是,线条绘制在椭圆的左上方。我想使用Ellipse的中心。因此,我必须将Ellipse#width / 2添加到x属性。但是我该怎么做呢?
答案 0 :(得分:2)
您可以使用IValueConverter
在Binding
时更改/转换值。
Canvas Dependencies = new Canvas();
Ellipse e1 = new Ellipse() { Width = 200, Height = 200, Stroke = Brushes.Red, StrokeThickness = 1 };
Ellipse e2 = new Ellipse() { Width = 200, Height = 200, Stroke = Brushes.Red, StrokeThickness = 1 };
Line l = new Line();
l.Stroke = Brushes.Green;
l.StrokeThickness = 3;
Binding x1 = new Binding(); x1.Path = new PropertyPath(Canvas.LeftProperty); x1.Converter = new MyConverter(); x1.ConverterParameter = e1;
Binding y1 = new Binding(); y1.Path = new PropertyPath(Canvas.TopProperty); y1.Converter = new MyConverter(); y1.ConverterParameter = e1;
Binding x2 = new Binding(); x2.Path = new PropertyPath(Canvas.LeftProperty); x2.Converter = new MyConverter(); x2.ConverterParameter = e2;
Binding y2 = new Binding(); y2.Path = new PropertyPath(Canvas.TopProperty); y2.Converter = new MyConverter(); y2.ConverterParameter = e2;
x1.Source = y1.Source = e1;
x2.Source = y2.Source = e2;
l.SetBinding(Line.X1Property, x1);
l.SetBinding(Line.Y1Property, y1);
l.SetBinding(Line.X2Property, x2);
l.SetBinding(Line.Y2Property, y2);
Dependencies.Children.Add(e1);
Dependencies.Children.Add(e2);
Dependencies.Children.Add(l);
SizeChangedEventHandler act = (Object s, SizeChangedEventArgs args) =>
{
BindingOperations.GetBindingExpressionBase(l, Line.X1Property).UpdateTarget();
BindingOperations.GetBindingExpressionBase(l, Line.Y1Property).UpdateTarget();
BindingOperations.GetBindingExpressionBase(l, Line.X2Property).UpdateTarget();
BindingOperations.GetBindingExpressionBase(l, Line.Y2Property).UpdateTarget();
};
e1.SizeChanged += act;
e2.SizeChanged += act;
Canvas.SetLeft(e1, 200);
Canvas.SetTop(e1, 200);
Canvas.SetLeft(e2, 500);
Canvas.SetTop(e2, 500);
Grid2.Children.Add(Dependencies);
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Ellipse e = parameter as Ellipse;
Double d = (Double)value;
return d + (e.ActualWidth / 2);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Ellipse e = parameter as Ellipse;
Double d = (Double)value;
return d - (e.ActualWidth / 2);
}
}
请注意,转换器仅考虑Ellipse.Width。您需要对其进行修改才能使其正常工作。
答案 1 :(得分:1)
您的绑定现在取决于两个属性,Canvas.Left(或Canvas.Top)和Ellipse.ActualWidth(或高度)。要实现此目的,您可以使用多重绑定。请参阅以下示例:
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-multibindings
然而,还有其他可能更简单的替代品。你可以使用渲染变换将椭圆转换为宽度的一半的X位置和高度的一半的Y位置,使椭圆居中于Canvas.Left和canvas.Top
问候,科林E.