首页 文章

WPF Treeview工具提示 - 动态

提问于
浏览
0

这是关于MVVM和WPF - 基于WPF Treeviewitems上的鼠标位置显示动态工具提示 . 假设当用户将鼠标悬停在已经获得某些业务数据的节点上时,它应显示工具提示 .

示例:如果我将鼠标悬停在经理项目上,请说 - “已锁定”,其他人则为“准备编辑”等 . 取决于鼠标项目数据.tooltip文本应指导用户做进一步的操作 . 我试图在TreeViewitem的Tooltip打开事件的帮助下进行工具提示文本的一些VM设置,并尝试更新工具提示文本..但面临一些问题 .

什么是最好和最简单的方法 . 尝试了行为并最终出现了一些错误 .

System.Windows.Markup.XamlParseException occurred
  Message="Cannot add content of type 'x.x.Views.CustomTreeView.TreeTooltipBehavior' to an object of type 'System.Windows.Interactivity.BehaviorCollection'.  Error at object 'x.x.x.Views.CustomTreeView.TreeTooltipBehavior' in markup file 'x.x.x.Views;component/mypage.xaml' Line 72 Position 53."
  Source="PresentationFramework"

码:

<MyMyControls:ExtendedTreeView x:Name="MyTreeView" ItemsSource="{Binding MyDriveCollection}"
             ItemContainerStyle="{StaticResource TVStyleTemplate}">
            <MyMyControls:ExtendedTreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type NavModel:TreeDataItem}" ItemsSource="{Binding MyDriveCollection}">
                    <MyControls:SimpleEditableTextBlock x:Name="TabLabel" Text="{Binding Path=MenuText, Mode=TwoWay}" 
                          ToolTip="{Binding MenuText,Mode=TwoWay}">
                    </MyControls:SimpleEditableTextBlock>
                </HierarchicalDataTemplate>
            </MyControls:ExtendedTreeView.ItemTemplate>
            <MyControls:ExtendedTreeView.ContextMenu>
                <ContextMenu ItemsSource="{Binding ContextMenuItems}">
                    <ContextMenu.ItemTemplate>
                        <DataTemplate>
                            <MenuItem Header="{Binding MenuText}"
                                      CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, 
                                      AncestorType={x:Type TreeView}},Path=SelectedItem}" Command="{Binding Command}">
                            </MenuItem>
                        </DataTemplate>
                    </ContextMenu.ItemTemplate>
                </ContextMenu>
            </MyControls:ExtendedTreeView.ContextMenu>
            <i:Interaction.Behaviors>
                <CustomTreeView:TreeTooltipBehavior CustomTreeView:ToolTipOpeningCommand="{Binding ToolTipOpeningCommand,Mode=TwoWay,diag:PresentationTraceSources.TraceLevel=High}" />
                <CustomTreeView:WorkspaceTreeViewContextMenuBehavior ContextMenuOpeningCommand="{Binding ContextMenuOpeningCommand}"/>
            </i:Interaction.Behaviors>
 </MyControls:ExtendedTreeView>

TreeTooltipBehavior.cs

public class TreeTooltipBehavior : Behavior<ExtendedTreeViewItem>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.ToolTipOpening += new ToolTipEventHandler(AssociatedObject_ToolTipOpening);
        }

        void AssociatedObject_ToolTipOpening(object sender, ToolTipEventArgs e)
        {
            if (sender != null)
            {
                TreeDataItem hit = ((TreeDataItem) (((FrameworkElement) (sender)).DataContext));

                if (ToolTipOpeningCommand != null)
                {
                    ToolTipOpeningCommand.Execute(hit);
                }
            }
        }

        public static readonly DependencyProperty ToolTipOpeningCommandProperty = DependencyProperty.Register(
            "ToolTipOpeningCommand",
            typeof(ICommand),
            typeof(TreeTooltipBehavior),
            new PropertyMetadata(null));

        public ICommand ToolTipOpeningCommand
        {
            get { return (ICommand)GetValue(ToolTipOpeningCommandProperty); }
            set { SetValue(ToolTipOpeningCommandProperty, value); }
        }

    }

在我的视图模型中,我期望处理ToolTipOpeningCommand并声明足以通过Behavior类获取事件 .

有趣的是contextmenu行为工作正常,但工具提示行为抛出xaml解析器异常..

1)我是在正确的地方(行为)定义的吗? 2)如果Contextmenu行为有效,为什么不工具提示? 3)粘贴在顶部的例外的任何线索?

我期望(Xaml)-tooltip behavior - >在(行为类) - >中调用tooltipopening事件,然后调用ViewModel中定义的命令 . 我为上下文菜单尝试了类似的方法并且工作正常 .

请提供一些有关解决此问题的提示 .

1 回答

  • 0

    XAML解析器错误是因为您尝试将仅适用于 ExtendedTreeViewItemBehavior 附加到 ExtendedTreeView 元素 . 换句话说,如果将 Behavior<ExtendedTreeViewItem> 更改为 Behavior<ExtendedTreeView> ,则将修复解析错误 .

    虽然我们无法从您提供的代码中看到, ContextMenu 的工作原因可能是因为 WorkspaceTreeViewContextMenuBehavior 派生自 Behavior ,其泛型类型参数与 ExtendedTreeView 兼容,例如 FrameworkElement .

相关问题