WPF树型视图上下文菜单Unhighlights项目 [英] WPF TreeViewItem Context Menu Unhighlights Item

查看:318
本文介绍了WPF树型视图上下文菜单Unhighlights项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有与此有一段时间了,现在的问题,并提出了一些低于理想的解决方案。问题是,打开一个树型视图的上下文菜单时,树型视图是灰色的。是否有可能留突出了树型视图,而其文本菜单是开放的?

I have been having problems with this for some time now, and have come up with some less-than-desirable solutions. The problem is that when a TreeViewItem's context menu is opened, the TreeViewItem is greyed out. Is it possible for a TreeViewItem to stay highlighted while its ContextMenu is open?

与树型视图花白出了问题,是可以让没有关系,上下文菜单和树型视图,它看起来丑陋。

The problem with the TreeViewItem greying out, is that it gives no relation to the context menu and the TreeViewItem, and it looks ugly.

一般情况下,我使用的设置上下文菜单中的代码是这样的。有时,上下文菜单将通过与PreviewRightMouseButtonDown EventSetter代码生成,但它不会有所作为:

Generally, the code I use for setting a context menu is this. Sometimes the context menu will be generated by the code with a PreviewRightMouseButtonDown EventSetter, but it doesn't make a difference:

    <TreeView>
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu>
                            <MenuItem Header="Menu Item 1" />
                            <MenuItem Header="Menu Item 2" />
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
        <TreeViewItem Header="Item 1">
            <TreeViewItem Header="Sub-Item 1"/>
        </TreeViewItem>
        <TreeViewItem Header="Item 2"></TreeViewItem>
    </TreeView>



到目前为止,我已经找到了唯一的解决办法是重写灰色聚焦颜色聚焦颜色,但随后的TreeView控件似乎永远不会消沉,当点击另一个控制等。我曾与列表视图和问题。

So far the only solution I have found is to override the "grey" unfocused color with the focused color, but then the TreeView never seems unfocused, such as when another control is clicked on. I have had problems with ListViews as well.

推荐答案

WPF的默认行为是改变树型视图的文本菜单打开时为灰色,但像几乎所有在WPF其他可以替代的:

WPF's default behavior is to change the TreeViewItem to gray when the ContextMenu opens, but like virtually everything else in WPF you can override this:


  1. 创建附加属性ContextMenuOpened

  2. 在在树型视图样式,绑定ContextMenuOpened为ContextMenu.IsOpen

  3. 添加时ContextMenuOpened和I​​sSelected是改变刷触发双方真实

下面的附加属性:

public class TreeViewCustomizer : DependencyObject
{
  public static bool GetContextMenuOpened(DependencyObject obj) { return (bool)obj.GetValue(ContextMenuOpenedProperty); }
  public static void SetContextMenuOpened(DependencyObject obj, bool value) { obj.SetValue(ContextMenuOpenedProperty, value); }
  public static readonly DependencyProperty ContextMenuOpenedProperty = DependencyProperty.RegisterAttached("ContextMenuOpened", typeof(bool), typeof(TreeViewCustomizer));
}

下面是在风格二传手:

<Setter Property="my:TreeViewCustomizer.ContextMenuOpened"
        Value="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource Self}}" />

下面是触发:

<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="IsSelected" Value="true"/>
    <Condition Property="my:TreeViewCustomizer.ContextMenuOpened" Value="true"/>
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  <Setter Property="Foreground"
          Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</MultiTrigger>

工作原理:每次文本菜单打开它的IsOpen属性设置。结合导致你的附加属性要在树型视图设置。这与IsSelected结合起来,调用它改变了前景和背景颜色,使仍出现选择的项目触发。

How it works: Every time the ContextMenu opens its IsOpen property is set. The binding causes your attached property to be set on the TreeViewItem. This, combined with IsSelected, invokes the trigger which changes the foreground and background colors to make the item still appear selected.

这篇关于WPF树型视图上下文菜单Unhighlights项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆