0

これは、MVVM と WPF に関するもので、WPF ツリービュー項目上のマウスの位置に基づいて動的なツールチップを表示します。ユーザーが、ツールチップを表示する必要があることに基づいてビジネスデータを取得したノードにカーソルを合わせるとします。

例: 管理項目にカーソルを合わせると、"ロックされています" と表示され、他の項目は "編集可能" と表示されます。項目データ上のマウスに依存します。ツールチップ テキストは、ユーザーがさらにアクションを実行するようにガイドする必要があります。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 クラスを介してイベントを取得するのに十分な宣言を行うことを期待しています。

興味深いことに、コンテキストメニューの動作は正常に機能しますが、ツールチップの動作は xaml パーサー例外をスローします..

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

私は、(Xaml)-tooltip 動作 -> (behavior クラス) で tooltipopening イベントを呼び出す -> ViewModel で定義されたコマンドを呼び出すことを期待しています。コンテキストメニューに対してこれと同様の方法を試してみましたが、うまくいきました。

この問題を解決するためのヒントを提供してください。

4

1 に答える 1

0

XAML パーサー エラーは、要素Behaviorにのみ適用されるをアタッチしようとしているためです。つまり、 に変更すると、解析エラーが修正されます。ExtendedTreeViewItemExtendedTreeViewBehavior<ExtendedTreeViewItem>Behavior<ExtendedTreeView>

提示されたコードからはわかりませんが、ContextMenu動作する理由はおそらく、と互換性のあるジェネリック型パラメーターを持つからWorkspaceTreeViewContextMenuBehavior派生するためです。BehaviorExtendedTreeViewFrameworkElement

于 2011-01-19T04:39:16.013 に答える