这是基于WPF TreeViewItems上鼠标位置的MVVM&WPF -Showing Dynamic Tooltip。假设用户徘徊在一个基于某些业务数据的节点上时,它应该显示工具提示。

示例:假设我是否悬停在管理器项目上。.应该说 - “锁定”,为他人“准备编辑” ..等。取决于鼠标的项目数据。ToolTip文本应指导用户采取进一步的操作。我正在尝试借助TreeViewItem的工具提示开放事件来执行工具提示文本的一些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); }
        }

    }

在我的视图模型中,我希望能够处理工具量命令,并声明足够多,以通过行为类获得事件。

有趣的是,ContextMenu行为效果很好,但工具提示行为会引发XAML解析器异常。

1)我在正确的位置定义(行为)吗? 2)如果ContextMenu行为起作用,那么为什么不工具呢? 3)从顶部粘贴的例外有任何线索吗?

我期望(XAML)-tooltip行为 - >在(行为类) - >中调用工具交易事件 - >>又调用ViewModel中定义的命令。我尝试了这种类似的上下文菜单方式,并且正常工作。

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

有帮助吗?

解决方案

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

尽管我们看不到您提出的代码,但原因是 ContextMenu 作品可能是因为 WorkspaceTreeViewContextMenuBehavior 源自a Behavior 具有与之兼容的通用类型参数 ExtendedTreeView, , 如 FrameworkElement.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top