我試圖將工具提示的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一旦它的附加的東西......它總是設置爲文本框。我嘗試過使用VisualTree獲取不同UIElements的多種方式。似乎沒有任何工作......所以我假設我不理解或錯過某些東西。
真正讓我感到意外的是,如果我進入我的代碼並查看工具提示的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.
}
}
我在做什麼不正確或不理解?
上下文編輯:自定義剪切效果基本上只是找到最接近的工具提示目標的祖先窗口,並使用它來確保工具提示永遠不會超出該窗口的範圍。
WPF的工具提示與PlacementTarget和DataContext一起工作的方式有些雜亂。它獲得配置快速使用;看看在https://stackoverflow.com/questions/6783693/how-to-set-a-placementtarget-for-a-wpf-tooltip-without-messing-up-the-datacontex –
你的視覺樹如何定義?工具提示駐留在自己的可視化樹中,並且無法使用RelativeSource在父窗口中查找某些內容。 – mm8
@TimothyGroote謝謝,我會檢查出來。對於mm8,我認爲RelativeSource值首先會定位到默認目標(在這種情況下是TextBox),然後向上移動它們的可視化樹來查找第一個Grid(可能需要將AnscestorLevel設置爲1)。但是,無論如何,我無法真正設置PlacementTarget。 – user2079828