
上图是单击文档上的文本框时 MS Word 2010 中的选项菜单示例。我正在尝试使用 WPF 中的装饰器来实现类似的东西,并且目前正在努力实现。
我创建了 2 个装饰器,称为 optionsButtonAdorner 和 AdvancedOptionsAdorner。当我单击画布上的项目时,我会显示带有一点滑动动画的 optionsButtonAdorner,当我单击 optionsButtonAdorner 时,我会显示带有相同滑动动画的 AdvancedOptionsAdorner。我让上面的任务正常工作,我的系统正确地呈现了两个装饰器,就像上图一样。
但复杂的部分是,如果我尝试在 AdvancedOptionsAdorner 中放置任何东西,我可以在装饰器中显示一个按钮,但我没有在按钮上获得任何 HitTest(与任何其他控件相同,例如如果我尝试放置一个我无法获得的测试框专注于它或与之互动)。如果我使用 snoop 查看对象,我可以看到按钮和文本框已启用并且 hittest 设置为 true。但是再进一步看,我实际上是在画布 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; }
}
Run Code Online (Sandbox Code Playgroud)
以下是装饰器的数据模板
<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>
Run Code Online (Sandbox Code Playgroud)
下面是我在实现装饰器后得到的渲染。

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

我错误地覆盖了 GetVisualChild 和 VisualChildrenCount 方法。相反,我在 adorner 类中创建了一个 VisualCollection 属性,并将我的 contentPresenter 添加到该集合中,而覆盖只是返回了预期的结果,如 VisualCollection[i] 用于 VisualChild 和 visualCollection.Count 用于 VisualChildrenCount。
此外,我对 ui 交互性没有任何问题,因为我将我的 Item 的 dataContext 传递给装饰器,而且进入 AdvancedOptions 装饰器的大多数命令都是 Prism Composite 命令,它们在相关的 ViewModel 中被触发
这是一些令人印象深刻的项目代码 - 我的赞美!不幸的是,我认为装饰器 - 虽然对于许多“装饰”目的非常有用,但不会在这里使用。您需要与此控件进行完全交互,而 Adorner 并不打算提供这种功能。这里,您需要的是常规的用户体验元素。装饰是作为一种独立的层实现的,与 UIElement 树的其余部分不同。在意识到这一点之前,我自己也曾沿着这条路走过。顺便说一句,我很想更多地了解您的项目,以及您使用 WPF 实现该项目的成功程度。如果我能帮上什么忙,请联系我。并致以最美好的祝愿。
| 归档时间: |
|
| 查看次数: |
548 次 |
| 最近记录: |