質問

これは、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); }
        }

    }

私のビューモデルでは、ToolTipopeningCommandを処理することを期待しており、Behaviorクラスを介してイベントを取得するのに十分宣言しました。

興味深いことに、ContextMenuの動作は正常に機能しますが、ツールチップの動作はXAMLパーサーの例外をスローします。

1)私は(行動のために)適切な場所で定義されていますか? 2)ContextMenuの動作が機能する場合、ToolTipBehaviorしてみませんか? 3)上部に貼り付けられた例外からの手がかりはありますか?

(xaml)-tooltipの動作 - >(動作クラス) - >でTooltipopeningイベントを呼び出すことを期待しています。コンテキストメニューのためにこの同様の方法を試してみましたが、正常に動作します。

PLSは、この問題の修正に関するヒントを提供します。

役に立ちましたか?

解決

XAMLパーサーエラーは、あなたが添付しようとしているからです Behavior それはのみ適用されます ExtendedTreeViewItemExtendedTreeView エレメント。言い換えれば、あなたが変更した場合 Behavior<ExtendedTreeViewItem>Behavior<ExtendedTreeView>, 、解析エラーを修正します。

あなたが提示したコードからはわかりませんが、理由は ContextMenu 作品はおそらくそのためです WorkspaceTreeViewContextMenuBehavior aから派生します Behavior 互換性のある汎用型パラメーターを使用します ExtendedTreeView, 、 そのような FrameworkElement.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top