【问题标题】:WPF Tooltip BindingWPF 工具提示绑定
【发布时间】:2011-09-19 02:17:07
【问题描述】:

我只有两周的时间进入 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>

谢谢。

【问题讨论】:

    标签: wpf data-binding tooltip hierarchicaldatatemplate


    【解决方案1】:

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

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

    相反,您要做的是创建工具提示的特定实例,并为其分配一个样式以设置其DataContext(非常重要;这就是您可以绑定到其“放置”的数据源属性的方式目标”,即显示工具提示的控件)及其Template。然后将ToolTip 的可视化树,包括绑定,放入模板中。最后,在您的控件中引用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 使用这种样式,它从TextBox 的绑定源的ValidationError 属性中获取其内容:

    <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 的可视化树中明确设置。但我可能没有想到什么重要的事情。

    【讨论】:

    • 提示:无论您是将其放入本地资源 (UserControl.Resources) 还是全局字典,请确保在 之前定义
    • 您也可以使用动态引用而不是静态引用,但是是的,通常您希望在使用它们之前定义样式。
    • 出色的答案,谢谢!甚至不需要样式,您可以直接将绑定应用到 DataContext 属性。
    【解决方案2】:
    <Label Content={Binding Path=Id} ToolTip={Binding Path=Name}/>
    

    试试这个

    【讨论】:

    【解决方案3】:

    这是一个适用于 kaxaml 的示例,其中包含一个比文本更详细的工具提示:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2010-10-22
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 2021-04-01
      相关资源
      最近更新 更多