c# - 无法与装饰层上的项目交互

标签 c# wpf adorner

enter image description here

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

我创建了 2 个装饰器,分别称为 optionsButtonAdorner 和 AdvancedOptionsAdorner。当我在 Canvas 上单击我的项目时,我会显示带有小滑动动画的 optionsButtonAdorner,当我单击 optionsButtonAdorner 时,我会显示带有相同滑动动画的 AdvancedOptionsAdorner。我让上述任务正常工作,并且我的系统正确呈现两个装饰器,就像上图一样。

但复杂的部分是,如果我尝试在 AdvancedOptionsAdorner 中放置任何东西,我可以在装饰器中显示一个按钮,但我没有在按钮上获得任何 HitTest(与任何其他控件相同,就像我尝试放置一个测试框一样我无法专注于它或与之互动)。如果我使用 snoop 查看对象,我可以看到按钮和文本框已启用并且 hittest 设置为 true。但进一步看,我实际上是在 Canvas Iteself 上获取 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>

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

enter image description here

编辑-

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

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

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

最佳答案

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

关于c# - 无法与装饰层上的项目交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23055883/

相关文章:

wpf - 具有记录和播放功能的基于 Windows 的 WPF 应用程序的 UI 自动化工具

wpf - 有什么办法可以让 WPF Adorner 忽略鼠标?

c# - WPF 中的 BUG?当 FlowDirection 为 RightToLeft 时,Adorner 中的 TextBlock 会显示反转文本

c# - 装饰器内的动画(调用 OnRender)

c# - 向 ASP.NET WebApi 2.2 添加身份验证

c# - AutoMapper - 条件映射

c# - 对 UserControl 进行验证工作

wpf - (MVVM-light) style.triggers 不起作用

c# - 访问 List<List<string>> 的元素

c# - 正确设置进程工作目录