WPF工具提示绑定

Nat*_*han 8 data-binding wpf tooltip hierarchicaldatatemplate

我只有两个星期的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>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Rob*_*ney 22

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

<TextBox.ToolTip>
   <StackPanel>
      ...put bound controls here
   </StackPanel>
</TextBox.ToolTip>
Run Code Online (Sandbox Code Playgroud)

相反,你要做的是创建一个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>
Run Code Online (Sandbox Code Playgroud)

它使用这个ToolTip:

<ToolTip x:Key="ErrorToolTip" Style="{StaticResource ErrorToolTipStyle}"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我不确定这一点,但我认为实际上必须在风格中DataTrigger设置上述的唯一部分是设置DataContext; 我想大多数其他事情都可以在ToolTip视觉树中明确设置.但我可能没想到重要的事情.


Kis*_*mar 18

<Label Content={Binding Path=Id} ToolTip={Binding Path=Name}/>
Run Code Online (Sandbox Code Playgroud)

试试吧

  • 但是,对于更详细的工具提示我该怎么办?一种在其中显示多个属性。 (2认同)

小智 5

这是一个 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>
Run Code Online (Sandbox Code Playgroud)