2011-01-18 1 views
0

이것은 MVVM &과 관련이 있습니다. WPF - WPF TreeviewItems에서 마우스 위치를 기반으로하는 동적 툴팁 표시. 사용자가 도구 설명을 표시해야하는 비즈니스 데이터가있는 노드를 사용자가 가리킬 때를 예로 들어 보겠습니다.WPF Treeview 툴팁 - 동적

예 : 관리자 항목에 마우스를 올리면 '잠김'이라고 말하고 다른 사람에게는 '편집 가능'등 .... 항목 데이터 위에 마우스를 놓은 상태에 따라 달라집니다. 툴팁 텍스트는 사용자가 추가 작업을 수행하도록 유도해야합니다. 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 동작이 작동하면 왜 tooltipbehavior가 작동하지 않습니까? 3) 상단에 붙여 넣은 예외의 단서가 있습니까?

(Xaml) -tooltip 동작 -> (동작 클래스)에서 tooltipopening 이벤트를 호출하여 ViewModel에 정의 된 명령을 호출합니다. 상황에 맞는 메뉴에서도 이와 비슷한 방식으로 시도해 보았습니다.

Pls는이 문제를 해결하는 방법에 대한 몇 가지 힌트를 제공합니다.

답변

0

ExtendedTreeViewItem에만 적용되는 BehaviorExtendedTreeView 요소에 연결하려고했기 때문에 XAML 파서 오류가 발생했습니다. 즉, Behavior<ExtendedTreeViewItem>Behavior<ExtendedTreeView>으로 변경하면 구문 분석 오류가 수정됩니다. 우리는 당신이 제시 한 코드에서 볼 수 있지만 WorkspaceTreeViewContextMenuBehaviorFrameworkElementExtendedTreeView와 호환되는 제네릭 형식 매개 변수와 Behavior에서 파생 때문에

는 이유 ContextMenu 작품은 아마이다.

+0

내 문제를 해결하는 데 시간을내어 주셔서 감사합니다. 문제는 지금 스타일로 해결됩니다 ...하지만 Treeviewitem tooltipopening 명령 behvior 첨부 할 수없는 이유를 모르겠다. – Maheshk