WPF:如何将线条绑定到 UI 元素?
我使用此方法将一条 Line 绑定到两个 ScatterViewItems 的中心:
private void BindLineToScatterViewItems(Shape line, ScatterViewItem origin, ScatterViewItem destination)
{
// Bind line.(X1,Y1) to origin.ActualCenter
BindingOperations.SetBinding(line, Line.X1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.Y") });
// Bind line.(X2,Y2) to destination.ActualCenter
BindingOperations.SetBinding(line, Line.X2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.Y") });
}
但现在我想将其从一个 ScatterViewItem 的底部绑定到另一个 ScatterViewItem 的顶部:
我怎样才能实现这一目标?
I use this method to bind a Line to the center of two ScatterViewItems:
private void BindLineToScatterViewItems(Shape line, ScatterViewItem origin, ScatterViewItem destination)
{
// Bind line.(X1,Y1) to origin.ActualCenter
BindingOperations.SetBinding(line, Line.X1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.Y") });
// Bind line.(X2,Y2) to destination.ActualCenter
BindingOperations.SetBinding(line, Line.X2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.Y") });
}
But now I'd like to bind it from the bottom from one ScatterViewItem to the top of the another ScatterViewItem:
How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以:
使用一个
IValueConverter
来获取视图项的边界矩形和一个转换器参数来指定计算中心的一侧。您将使用的转换器是:
保留相同的
ActualCenter
绑定,但将该行的ZIndex
设置在矩形的下面。使用这种方法可能会让您不必检测一个ScatterViewItem
的移动方式是否需要更改转换器中使用的一侧。You could:
Use a
IValueConverter
that takes the bounding rectangle of the view item and a converter parameter to specify the side to calculate the center from.The converter you would use would be:
Keep the same
ActualCenter
bindings, but make theZIndex
of the line below that of the rectangles. Using this approach will likely keep you from having to detect if oneScatterViewItem
moves in such a way that needs to change the side used in the converter.有什么原因无法设置转换器吗?
is there a reason this wouldnt work to set the converter?