这是关于 MVVM 和 WPF - 根据 WPF Treeviewitems 上的鼠标位置显示动态工具提示。假设当用户将鼠标悬停在具有一些业务数据的节点上时,它应该显示工具提示。
示例:假设如果我将鼠标悬停在管理器项目上..应该说 -“锁定”,对于其他人“准备编辑”..等等。取决于鼠标悬停在项目数据上。工具提示文本应引导用户执行进一步操作。我正在尝试借助 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>
树工具提示行为.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 类获取事件。
有趣的是,上下文菜单行为工作正常,但工具提示行为引发 xaml 解析器异常..
1)我是否在正确的地方定义(行为)?2)如果 Contextmenu 行为有效,那么为什么不使用 tooltipbehavior ?3)粘贴在顶部的异常的任何线索?
我期望(Xaml)-tooltip 行为-> 在(行为类)中调用 tooltipopening 事件-> 这反过来又调用 ViewModel 中定义的命令。我为上下文菜单尝试了这种类似的方式并且工作正常。
请提供一些有关解决此问题的提示。