WPF 工具提示绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2212171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Tooltip Binding
提问by Nathan
I am only two weeks into WPF so this is probably a trivial question. I have a collection "CellList" which has a few properties I would like to bind to a ToolTip
so when I hover over a label information from the current instance of CellList
is displayed. How do I do that? I understand simple binding and this maybe simple binding too but I can't wrap my head around it. Below is my XAML for the label. Could someone explain to me how I can accomplish this.
我只有两周的 WPF 所以这可能是一个微不足道的问题。我有一个集合“CellList”,它有一些我想绑定到的属性,ToolTip
所以当我将鼠标悬停在CellList
显示当前实例的标签信息上时。我怎么做?我理解简单绑定,这也可能是简单绑定,但我无法理解它。下面是我的标签 XAML。有人可以向我解释我如何做到这一点。
<HierarchicalDataTemplate>
<ListBox ItemsSource="{Binding CellList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content=" " Height="20" Width="15" Background="{Binding Path=ExptNameBkg, Converter={StaticResource ExptNameToBrushConverter}}" BorderBrush="Black" BorderThickness="1" >
</Label>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</HierarchicalDataTemplate>
Thanks.
谢谢。
回答by Robert Rossney
The tricky thing about ToolTip
s is that a ToolTip
is an object you associate with a control, and not part of the control's visual tree. So you can't populate it the way you'd populate things in the visual tree, e.g.:
关于ToolTip
s的棘手之处在于 aToolTip
是您与控件关联的对象,而不是控件的可视化树的一部分。所以你不能用你在可视化树中填充东西的方式来填充它,例如:
<TextBox.ToolTip>
<StackPanel>
...put bound controls here
</StackPanel>
</TextBox.ToolTip>
Instead, what you have to do is create a specific instance of a ToolTip, and assign it a style that sets its DataContext
(very important; that's how you can bind to the properties of the data source of its "placement target," i.e. the control that's displaying the tooltip) and its Template
. Then put the visual tree of the ToolTip
, including bindings, into the template. Finally, reference the ToolTip
in your control.
相反,您需要做的是创建一个 ToolTip 的特定实例,并为其分配一个样式来设置它的DataContext
(非常重要;这就是您可以绑定到其“放置目标”的数据源的属性的方式,即控件即显示工具提示)及其Template
. 然后将 的可视化树ToolTip
,包括绑定,放入模板中。最后,ToolTip
在您的控件中引用。
So, here's a TextBox
whose Binding
does validation:
所以,这里有一个TextBox
whoBinding
验证:
<TextBox ToolTip="{StaticResource ErrorToolTip}">
<TextBox.Text>
<Binding Source="SourceProperty">
<Binding.ValidationRules>
<DataErrorValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
It uses this ToolTip
:
它使用这个ToolTip
:
<ToolTip x:Key="ErrorToolTip" Style="{StaticResource ErrorToolTipStyle}"/>
And the ToolTip
uses this style, which gets its content from the ValidationError
property of the TextBox
's binding source:
并且ToolTip
使用这种样式,它从的绑定源的ValidationError
属性中获取其内容TextBox
:
<Style x:Key="ErrorToolTipStyle" TargetType="{x:Type ToolTip}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="HasDropShadow" Value="True"/>
<Setter Property="DataContext" Value="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border
Name="Border"
BorderThickness="1"
BorderBrush="LightGray">
<StackPanel Orientation="Vertical">
<Label Background="Firebrick" Foreground="White" FontWeight="Bold" Margin="4">Validation error</Label>
<TextBlock Margin="10" Text="{Binding ValidationError}"/>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="HasDropShadow" Value="true">
<Setter TargetName="Border" Property="CornerRadius" Value="4"/>
<Setter TargetName="Border" Property="SnapsToDevicePixels" Value="true"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'm not certain of this, but I think that the only part of the above that actually has to be set in the style is the DataTrigger
setting the DataContext
; I think most everything else could just be explicitly set in the ToolTip
's visual tree. But I'm probably not thinking of something important.
我不确定这一点,但我认为实际上必须在样式中DataTrigger
设置的上述唯一部分是设置DataContext
; 我认为大多数其他内容都可以在ToolTip
的可视化树中明确设置。但我可能没有想到重要的事情。
回答by Kishore Kumar
<Label Content={Binding Path=Id} ToolTip={Binding Path=Name}/>
just try this
试试这个
回答by Dr. WPF
Here's a kaxaml-ready example that includes a tooltip that is a little more elaborate than just text:
这是一个 kaxaml-ready 示例,其中包含一个比文本更复杂的工具提示:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="CharacterData">
<x:XData>
<Data xmlns="">
<Character First="Bart" Last="Simpson" Background="LightGreen" />
<Character First="Homer" Last="Simpson" Background="LightBlue" />
<Character First="Lisa" Last="Simpson" Background="Pink" />
<Character First="Maggie" Last="Simpson" Background="Yellow" />
<Character First="Marge" Last="Simpson" Background="PapayaWhip" />
</Data>
</x:XData>
</XmlDataProvider>
<ToolTip x:Key="ElaborateToolTip">
<Grid Margin="5">
<Rectangle RadiusX="6" RadiusY="6" Fill="{Binding XPath=@Background}" />
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="{Binding XPath=@First}" Margin="0,0,6,0" />
<TextBlock Text="{Binding XPath=@Last}" />
</StackPanel>
</Grid>
</ToolTip>
</Page.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource CharacterData}, XPath=Data/Character}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ToolTip" Value="{StaticResource ElaborateToolTip}" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@First}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Page>