我是新來的WPF,所以這可能是一個新手問題。我正在研究某種圖編輯器,並希望用線連接一些元素。其中一些元素將嵌套在其他元素中,並且一行可能會跨越不同級別的元素。所以我想要做的是在頂部父元素中的OnRender
事件中繪製這些行。這是一個初步的嘗試,我使用的按鈕周圍的廣場,其中線代替,只是爲了確保我能夠正確定位的子元素:TransformToAncestor給我錯誤的轉換
public class Container : Border
{
public readonly StackPanel Panel = new StackPanel();
private readonly Pen _Pen = new Pen(Brushes.Red, 2);
public Container()
{
Panel.Orientation = Orientation.Vertical;
Panel.Children.Add(MakeButton("One"));
Panel.Children.Add(MakeButton("Two"));
Panel.Children.Add(MakeButton("Three"));
Child = Panel;
}
private Rect GetRect(Visual parent, FrameworkElement element)
{
return element.TransformToAncestor(parent).TransformBounds(LayoutInformation.GetLayoutSlot(element));
}
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
foreach (Button item in Panel.Children)
{
var box = GetRect(this, item);
dc.DrawRectangle(Brushes.Transparent, _Pen, box);
}
}
private static Button MakeButton(string text)
{
Button button = new Button();
button.Content = text;
button.Padding = new Thickness(10);
button.Margin = new Thickness(5);
return button;
}
}
但這種結果我得到:
如果我在GetRect
法LayoutInformation.GetLayoutSlot(element)
取代element.TransformToAncestor(parent).TransformBounds(LayoutInformation.GetLayoutSlot(element))
,它會看的方式是應該的,但只是因爲繪製發生在按鈕的直接父發生。在我的實際應用程序中,直接父項不會執行繪圖,因此我需要能夠獲取相對於任意父項的槽。