WPF 工具提示绑定(bind)

标签 wpf data-binding tooltip hierarchicaldatatemplate

我只进入 WPF 两周,所以这可能是一个微不足道的问题。我有一个集合“CellList”,它有一些我想绑定(bind)到 ToolTip 的属性所以当我将鼠标悬停在当前 CellList 实例的标签信息上时被陈列。我怎么做?我理解简单的绑定(bind),这也可能是简单的绑定(bind),但我无法理解它。下面是我的标签的 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>

谢谢。

最佳答案

ToolTip 的棘手之处s 是一个 ToolTip是您与控件关联的对象,而不是控件的可视化树的一部分。所以你不能像在可视化树中填充东西的方式来填充它,例如:

<TextBox.ToolTip>
   <StackPanel>
      ...put bound controls here
   </StackPanel>
</TextBox.ToolTip>

相反,您需要做的是创建一个工具提示的特定实例,并为其分配一个样式以设置其DataContext。 (非常重要;这就是您可以绑定(bind)到其“放置目标”的数据源属性的方式,即显示工具提示的控件)及其Template .然后把 ToolTip 的可视化树,包括绑定(bind),到模板中。最后,引用 ToolTip在你的控制之下。

所以,这是一个 TextBox谁的Binding进行验证:
<TextBox ToolTip="{StaticResource ErrorToolTip}">
    <TextBox.Text>
        <Binding Source="SourceProperty">
            <Binding.ValidationRules>
               <DataErrorValidationRule/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

它使用这个 ToolTip :
<ToolTip x:Key="ErrorToolTip" Style="{StaticResource ErrorToolTipStyle}"/>

ToolTip使用这种风格,它的内容来自 ValidationError TextBox 的属性(property)的绑定(bind)源:
<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>

我不确定这一点,但我认为上面唯一需要在样式中设置的部分是 DataTrigger设置 DataContext ;我认为大多数其他内容都可以在 ToolTip 中明确设置。的视觉树。但我可能没有想到什么重要的事情。

关于WPF 工具提示绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212171/

相关文章:

c# - 在 WPF 的只读文本框中启用复制粘贴功能

wpf - 如何在 WindowStyle ="None"的 WPF 窗口中强制执行 MinWidth 和 MinHeight?

jQuery ajax 工具提示 : where to find a nice plugin?

jquery - Bootstrap 工具提示不起作用?

ListView 中的 Wpf 组合框绑定(bind)

c# - WPF 事件触发器更改其他 UI 元素

c# - xaml 条件字符串格式

.net - 如何区分 WinForm 控件上的数据绑定(bind)和用户操作

c# - 强类型 Windows 窗体数据绑定(bind)

HTML-工具提示相对于鼠标指针的位置