【发布时间】: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 复合命令,它们在相关视图模型
【问题讨论】: