如何为每个ListBoxItem设置ToolTip

use*_*848 2 wpf listbox tooltip itemtemplate

我有ListBox控制权;我将如何使用下面的代码设置ToolTip每个ListBoxItem

<ListBox Name="FillSelections" 
         VerticalContentAlignment="Stretch"
         Margin="1, 3, 1, 3"
         IsEnabled="True" 
         Grid.Column="0" 
         Background="Transparent"
         HorizontalContentAlignment="Center"
         SelectedItem="{Binding SelectedColor}"
         SelectionMode="Single"
         Style="{StaticResource HorizontalListBoxStyle}"
         ItemsSource="{Binding FillColors}"
         ItemTemplate="{StaticResource ColorsItemTemplate}">
</ListBox>

<DataTemplate x:Key="ColorsItemTemplate">
    <Border Width="20" 
            Height="16"
            BorderBrush="Black"
            BorderThickness="1">
        <Border.Background>
            <SolidColorBrush Color="{Binding}" />
        </Border.Background>
        <Path Stroke="Red" 
              StrokeThickness="3"
              x:Name="abc"
              Visibility="Hidden">
            <Path.Data>
                <LineGeometry StartPoint="0,16" EndPoint="20,0"/>
            </Path.Data>
        </Path>
    </Border>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="#00FFFFFF">
            <Setter TargetName="abc" Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

Cyb*_*axs 6

尝试这样的事情

<ListBox Width="400" Margin="10" 
         ItemsSource="{Binding Source={StaticResource myTodoList}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=TaskName}" 
                       ToolTipService.ToolTip="{Binding Path=TheTooltipText}"/>
        </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

当然,无论您的绑定源是什么,都可以调整 ItemsSource 绑定,并且可以使用您实际想要显示的列表中对象的任何公共属性来调整绑定 Path 部分。