2017-06-16 10 views
2

ビジュアルツリーの上にあるウィンドウにツールヒントのPlacementTargetを変更しようとしています。私はPlacementTargetを除いてすべてを魅了しました。 XAMLとコードの例を示します。どちらも動作しません。このスタイルは、現在TextBoxに添付されている単一のツールチップに使用されています。C#WPF:ツールチップのPlacementTargetを変更する

<Style TargetType="ToolTip"> 
    <Setter Property="ToolTipService.PlacementTarget" 
      Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
       AncestorType={x:Type Grid }} }" /> 
</Style> 

私は...コードに行くと、それが何かに添付いたらtooltip.PlacementTargetを見れば、それは常にテキストボックスに設定されています。私は、異なるUIElementsを取得するためにVisualTreeを使用する複数の方法を試みました。何も動作していないようです...私は何かを理解していないか、または見逃していないと仮定しています。

本当に私を得ることは、自分のコードに行き、ツールチップのPlacementTargetを見ると、他のものに設定することができないということです。たとえば:

var ancestors = toolTip.PlacementTarget.GetSelfAndAncestors(); 

foreach(var ancestor in ancestors) 
{ 
    if(var ancestor is Grid) 
    { 
     // Conditional always hits. 
     // Before this line, PlacementTarget is a TextBox. 
     toolTip.PlacementTarget = (UIElement)ancestor; 
     // After the line, PlacementTarget is still a TextBox. 
    } 
} 

私は間違っているのか、わからないのですか?

コンテキストの編集:カスタムクリッピングエフェクトは、基本的に、最も近い祖先ウィンドウをツールヒントのターゲットに配置し、それを使用してツールヒントがそのウィンドウの境界を超えないようにします。

+0

WPFは、ツールチップが 'PlacementTarget'と' DataContext'で動作する方法がちょっと面倒です。それは速く設定します。 https://stackoverflow.com/questions/6783693/how-to-set-a-placementtarget-for-a-wpf-tooltip-without-messing-up-the-datacontex –

+0

ビジュアルツリーの定義方法?ヒントは、独自のビジュアルツリーに存在し、親ウィンドウ内で何かを探すためにRelativeSourceを使用することはできません。 – mm8

+0

@TimothyGrooteありがとう、私はそれをチェックアウトします。 mm8では、RelativeSourceの値が最初にデフォルトのターゲット(この場合はTextBox)をターゲットにし、ビジュアルツリーを上に移動して最初のグリッドを探します(ただし、AnscestorLevelを1に設定する必要があります)。しかし、PlacementTargetを実際に設定するための値を取得することはできません。 – user2079828

答えて

2

のプロパティをPlacementTargetとして使用して、短いサンプルをTooltipに設定します。あなたの質問に答えるために

<Window x:Class="WpfApp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Tag="Bar"> 
    <Window.Resources> 
     <ToolTip x:Key="FooToolTip"> 
      <StackPanel> 
       <TextBlock Text="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/> 
      </StackPanel> 
     </ToolTip> 
    </Window.Resources> 
    <Grid> 
     <TextBlock 
      Text="Foo" 
      ToolTip="{StaticResource FooToolTip}" 
      ToolTipService.PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
      HorizontalAlignment="Center" VerticalAlignment="Center" Height="20" Width="50"> 
     </TextBlock> 
    </Grid> 
</Window> 

EDIT

最初のスニペットは、間違った方法でToolTipServiceを使用しています。

ToolTipServiceクラス添付プロパティが配置を決定するために使用されています、動作、およびツールチップの外観を制御します。 これらのプロパティは、ツールチップを定義する要素に設定されます。

スタイルで適用

:背後にあるコードでは2番目のスニペット用として

<Window x:Class="WpfApp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" 
     Tag="Bar"> 
    <Window.Resources> 
     <ToolTip x:Key="FooToolTip"> 
      <StackPanel> 
       <TextBlock Text="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/> 
      </StackPanel> 
     </ToolTip> 
     <Style x:Key="ToolTipStyle"> 
      <Setter Property="ToolTipService.ToolTip" Value="{StaticResource FooToolTip}"/> 
      <Setter Property="ToolTipService.PlacementTarget" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <TextBlock 
      Text="Foo" Style="{StaticResource ToolTipStyle}" 
      HorizontalAlignment="Center" VerticalAlignment="Center" Height="20" Width="50"> 
     </TextBlock> 
    </Grid> 
</Window> 

ToolTipが開いているとToolTipが閉じているときにPlacementTargetがnullになると、あなたはPlacementTargetを設定することはできません。 @ mm8が指摘しているように、ToolTipPlacementTargetは別のビジュアルツリーにあります。ToolTipは となり、Windowとなります。

+0

私はこれを見て、それは(おそらく)私の問題を解決するが、本当に質問に答えることはありません。 2つのコードスニペットについて、正確には何が間違っていますか? 2番目のプログラムでPlacementTargetをプログラムで設定できないのはなぜですか? – user2079828

+0

また、スタイルを操作したいすべてのコントロールにplacementTargetを入れないので、ツールチップスタイルで設定する必要があります。 – user2079828

+0

私はあなたの説明を使って私をつかまえました。私は、任意の特定の動作にxamlのプロパティをアタッチするビヘイビアの子クラスを作成することで解決できました(https://stackoverflow.com/questions/1647815/how-to-add-a-blend-behaviorに基づく)。 -in-a-style-setter)を使用します。次に、ウィンドウにタグを配置し、自分の振る舞いを使ってツールチップスタイルを設定します。タグに一致する最初の祖先を検索し、そのプロパティを使用してポップアップに合わせます。これにより、ビューごとに1つのツールチップスタイルを設定し、ツールチップを収めたいウィンドウにタグを配置することができます。個々のコントロールで作業する必要はありません。 – user2079828

関連する問題