画布具有带装饰器的元素。Adorner使用VisualCollection,并将拇指放置在装饰元素的特定位置。我想点击装饰器,处理画布的PreviewMouseLeftButtonDown事件,并获得装饰器内部的底层元素。但在本例中,我将MainWindow作为e.Source。你能帮帮忙吗?我几乎是WPF的新手。
public class ConnectorAdorner : Adorner
{
private readonly ConnectorThumb _thumb;
private readonly VisualCollection _visuals;
public ConnectorAdorner(UIElement adornedElement) : base(adornedElement)
{
Focusable = true;
_visuals = new VisualCollection(this);
_thumb = new ConnectorThumb();
_thumb.Background = Brushes.Transparent;
_visuals.Add(_thumb);
}
protected override Size ArrangeOverride(Size finalSize)
{
double elHeight = AdornedElement.DesiredSize.Height;
double adornerWidth = DesiredSize.Width;
double adornerHeight = DesiredSize.Height;
_thumb.Arrange(new Rect(0, -elHeight/2, adornerWidth, adornerHeight));
return finalSize;
}
protected override Visual GetVisualChild(int index)
{
return _visuals[index];
}
protected override int VisualChildrenCount
{
get { return _visuals.Count; }
}
}
public class Shape : UserControl
{
private AdornerLayer _adornerLayer;
public Shape()
{
Width = 50;
Height = 50;
Background = Brushes.Tomato;
BorderThickness = new Thickness(2);
BorderBrush = Brushes.Black;
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_adornerLayer = AdornerLayer.GetAdornerLayer(this);
if (_adornerLayer != null)
{
ConnectorAdorner adorner = new ConnectorAdorner(this);
_adornerLayer.Add(adorner);
}
}
}
public class ConnectorThumb : Thumb
{
public ConnectorThumb()
{
Width = 20;
Height = 20;
Background = Brushes.Transparent;
Foreground = Brushes.Black;
BorderThickness = new Thickness(1);
IsHitTestVisible = true;
}
}
public partial class MainWindow : Window
{
private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// I'd like to get ConnectionThumb here..
}
}发布于 2014-04-24 05:32:44
虽然很难在看不到代码的情况下说出来,但我认为在元素(Adorner)上使用Canvas.ZIndex属性将确保元素按您想要的方式分层。
https://stackoverflow.com/questions/23255458
复制相似问题