WPF:在 Expression Blend 4 中编辑上下文菜单或工具提示:错误“不能有逻辑或可视父级”。
我想根据 Expression Blend 4 中的默认设置为 ContextMenu 或工具提示创建新样式。为此,我执行以下操作(ContextMenu 的示例):
- 在 EBlend 4 中创建新的 WPF 应用程序。
- 将按钮放在窗口上。
- 为按钮创建新的上下文菜单。
- 将两个示例项目添加到 ContextMenu(带有标题,即“item1”和“item2”)。
- 在“属性”面板中,在“样式”属性下的按钮 ContextMenu 属性属性中,单击“样式”属性右侧的方块,然后选择“转换为新资源...”。
- 在打开的“创建样式资源”窗口中,为“此文档[窗口:窗口]”选择“定义于”或新的资源字典 - 没关系。
- 我在消息底部得到了 ContextMenu 默认样式的 XAML,如下所示。
- 我想通过预览编辑此样式,因此我转到“资源”选项卡,找到“ContextMenuStyle1”,右键单击并选择“编辑”。
- 我无法编辑样式,有信息抛出异常“'ContextMenu'不能有逻辑或视觉父级。”。
此外,我也无法向我的资源字典添加任何其他新样式,因为它显示“无法将资源添加到“ResourceDictionary1.xaml”,因为“ResourceDictionary1.xaml”包含阻止其更新的错误。”直到我关闭“ResourceDictionary1.xaml”视图并重新打开它。
任何人都可以解决这个问题并给出如何在 Expression Blend 4 中编辑 ContextMenu 或 ToolTip 样式并提供更改的可视化预览并访问其所有属性和功能的解决方案吗?
<Style x:Key="ContextMenuStyle1" TargetType="{x:Type ContextMenu}">
<Setter Property="Background" Value="{DynamicResource MenuBackgroundBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{DynamicResource WindowBorderBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Uid="Border_93">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.DropShadowKey}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="0,0,5,5"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="4" Opacity="0.8" ShadowDepth="1"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Uid="Border_50">
<ScrollViewer CanContentScroll="True" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}" Uid="ScrollViewer_9">
<ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Uid="ItemsPresenter_5"/>
</ScrollViewer>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I want to create new style for ContextMenu or Tooltip basing on it's defaults in Expression Blend 4. To do this I do the following (example for ContextMenu):
- Create new WPF application in EBlend 4.
- Put button on window.
- Create new ContextMenu for button.
- Add two example items to ContextMenu (with header i.e. "item1" and "item2").
- In "Properties" panel in buttons ContextMenu property properties under "Style" property click on the square on right side of "Style" property and choose "Convert to new resource...".
- In opened "Create style resource" window choose "defined in" for "This document [Window:Window]" or new Resource dictionary - it doesn't matter.
- I get XAML for default style of ContextMenu like this in the bottom of my message.
- I want to edit this style with preview, so I go to "Resources" tab, find my "ContextMenuStyle1", right click and choose "Edit".
- I cannot edit the style, have information that an Exception was thrown "'ContextMenu' cannot have a logical or visual parent.".
Additionally I cannot also add to my resource dictionary any other new styles, because it says "The resource cannot be added to "ResourceDictionary1.xaml", since "ResourceDictionary1.xaml" contains errors which prevent it of being updated." until I close "ResourceDictionary1.xaml" view and reopen it.
Can anybody solve the problem and give a solution on how to edit ContextMenu or ToolTip style in Expression Blend 4 with visual preview of changes and access to all it's properties and features?
<Style x:Key="ContextMenuStyle1" TargetType="{x:Type ContextMenu}">
<Setter Property="Background" Value="{DynamicResource MenuBackgroundBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{DynamicResource WindowBorderBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Uid="Border_93">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.DropShadowKey}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="0,0,5,5"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="4" Opacity="0.8" ShadowDepth="1"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Uid="Border_50">
<ScrollViewer CanContentScroll="True" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}" Uid="ScrollViewer_9">
<ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Uid="ItemsPresenter_5"/>
</ScrollViewer>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Microsoft Expression Blend 中的一个
bug
,请查看此链接:"'工具提示'不能有逻辑或视觉父级。"在 Blend 4 中编辑样式时That's a
bug
in Microsoft Expression Blend look at this link: "'Tooltip' cannot have a logical or visual parent." when editing style in Blend 4