WPF richTextBox无法识别为UIElement

时间:2013-07-22 09:55:36

标签: c# wpf richtextbox adorner uielement

我有以下代码将Adorner附加到我UIElement上的Canvas

private void slideCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  {
                selected = false;
                if (selectedElement != null)
                {
                    aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]);
                    selectedElement = null;
                }
            }

            if (e.Source != slideCanvas)
            {
                _isDown = true;
                _startPoint = e.GetPosition(slideCanvas);

                selectedElement = e.Source as UIElement;

                _originalLeft = Canvas.GetLeft(selectedElement);
                _originalTop = Canvas.GetTop(selectedElement);

                aLayer = AdornerLayer.GetAdornerLayer(selectedElement);
                aLayer.Add(new ResizingAdorner(selectedElement));
                selected = true;
                e.Handled = true;
            }
}

由于某些原因,当我在运行时单击RichTextBox时,程序崩溃,因为RichTextBox找不到e.Source UIElement

selectedElement只是null

有人可以告诉我为什么,请给我一个工作吗?

1 个答案:

答案 0 :(得分:1)

显然e.Source是您点击的RichTextBox的Document。它是FlowDocument,不是来自UIElement

但是,您可以通过FlowDocument的Parent属性访问RichTextBox。

if (e.Source is FlowDocument)
{
    selectedElement = ((FlowDocument)e.Source).Parent as UIElement;
}
else
{
    selectedElement = e.Source as UIElement;
}