【问题标题】:Cannot interact with items on the adorner layer无法与装饰层上的项目交互
【发布时间】:2014-04-14 08:53:41
【问题描述】:

上图是您单击文档上的文本框时 MS Word 2010 中的选项菜单示例。我正在尝试在 WPF 中使用装饰器来实现类似的东西,目前正在努力实现。

我创建了 2 个装饰器,称为 optionsButtonAdorner 和 AdvancedOptionsAdorner。当我在画布上单击我的项目时,我会显示带有一点滑动动画的 optionsButtonAdorner,当我单击 optionsButtonAdorner 时,我会使用相同的滑动动画显示 AdvancedOptionsAdorner。我使上述任务正常工作,并且我的系统正确地渲染了两个装饰器,就像上图一样。

但复杂的部分是,如果我尝试在 AdvancedOptionsAdorner 中放置任何东西,我可以在装饰器中显示一个按钮,但我没有在按钮上获得任何 HitTest(与任何其他控件相同,例如我尝试放置一个测试框我无法专注于它或与之互动)。如果我使用 snoop 查看对象,我可以看到按钮和文本框已启用并且 hittest 设置为 true。但是再进一步看,我实际上是在画布上获取 mousedown 事件,而不是装饰对象。现在我在想,如果我的方法本身是错误的。下面是 AdvancedOptionsAdorner 的示例代码

感谢您的帮助,对这么长的帖子感到抱歉。

public DesignerItemAdvancedOptionsAdorner(DesignerControl designerItem):base(designerItem)
        {
            _designerItem = designerItem;
            DataTemplate dataTemplate = (DataTemplate)FindResource("DesignerItemAdvancedOptionsAdorner");
            _contentPresenter = new ContentPresenter() { ContentTemplate = dataTemplate, Opacity = 0.75 };
            Loaded += DesignerItemAdvancedOptionsAdorner_Loaded;
            Unloaded += DesignerItemAdvancedOptionsAdorner_Unloaded;
        }
private void DesignerItemAdvancedOptionsAdorner_Loaded(object sender, RoutedEventArgs e)
        {
            double newDistance = Math.Round((_designerItem.ControlActualWidth * ActiveZoomLevel) + 50);
            AnimateMargin(new Thickness((_designerItem.ControlActualWidth * ActiveZoomLevel) + 45, 0, 0, 0), new Thickness(newDistance, 0, 0, 0), 0.1);
        }
protected override Visual GetVisualChild(int index)
        {
            return _contentPresenter;
        }
        protected override int VisualChildrenCount
        {
            get { return 1; }
        }

下面是装饰器的数据模板

<DataTemplate x:Key="DesignerItemAdvancedOptionsAdorner">
        <Grid RenderTransformOrigin="0.5,0.5" Margin="0,-10,0,0" Height="320" Width="160" HorizontalAlignment="Left">
            <Path Stroke="{DynamicResource ApplicationPrimaryColour}" StrokeThickness="1" Fill="White">
                <Path.Data>
                    <CombinedGeometry GeometryCombineMode="Union">
                        <CombinedGeometry.Geometry1>
                            <RectangleGeometry  Rect="0,0,140,280">
                                <RectangleGeometry.Transform>
                                    <TranslateTransform X="10" />
                                </RectangleGeometry.Transform>
                            </RectangleGeometry>
                        </CombinedGeometry.Geometry1>
                        <CombinedGeometry.Geometry2>
                            <PathGeometry>
                                <PathFigure StartPoint="0,20">
                                    <LineSegment Point="10,10" />
                                    <LineSegment Point="10,30" />
                                </PathFigure>
                            </PathGeometry>
                        </CombinedGeometry.Geometry2>
                    </CombinedGeometry>
                </Path.Data>
              </Path>
            <TextBlock Text="Options" HorizontalAlignment="Center" VerticalAlignment="Top" FontWeight="Bold" Margin="0,5,0,0"/>
            <Button HorizontalAlignment="Center" VerticalAlignment="Center" Height="40" Width="100" Content="Test"/>
        </Grid>
    </DataTemplate>

下面是我实现装饰器后得到的渲染。

编辑 -

设法让它在下面工作是我得到的渲染

我错误地覆盖了 GetVisualChild 和 VisualChildrenCount 方法。相反,我在 adorner 类中创建了一个 VisualCollection 属性并将我的 contentPresenter 添加到该集合中,并且覆盖只返回了预期的结果,例如 VisualCollection[i] 用于 VisualChild 和 visualCollection.Count 用于 VisualChildrenCount。

另外,我的 ui 交互性没有任何问题,因为我将我的 Item 的 dataContext 传递给装饰器,而且进入 AdvancedOptions 装饰器的大多数命令都是 Prism 复合命令,它们在相关视图模型

【问题讨论】:

    标签: c# wpf adorner


    【解决方案1】:

    这是一些令人印象深刻的项目代码 - 我的赞美!不幸的是,我认为装饰器 - 虽然对于许多“装饰”目的非常有用,但不会在这里服务。您需要与此控件完全交互,而 Adorner 并不打算提供。在这里,您需要的是常规的 UX 元素。装饰被实现为一种独立的层,与 UIElement 树的其余部分不同。在意识到这一点之前,我自己也曾走在这条路上。顺便说一句,我很想更多地了解您的项目,以及您使用 WPF 实现它的成功程度。如果我能帮上忙,请打我。并致以最良好的祝愿。

    【讨论】:

    • 感谢您对此感兴趣。我设法解决了这个问题,并在我对原始帖子的编辑中解释了这个问题。我在交互性方面没有问题,而且我在那个装饰器中使用复合命令。现在我只在里面放了复制和粘贴命令,但是现在代码可以工作了,我们可以放任何我们想要的东西,比如对齐、字体等
    猜你喜欢
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 2012-11-12
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多