Wpf 如何为每个ListBoxItem设置工具提示

Wpf 如何为每个ListBoxItem设置工具提示,wpf,listbox,tooltip,itemtemplate,Wpf,Listbox,Tooltip,Itemtemplate,我有一个列表框控件;如何使用下面的代码为每个ListBoxItem设置工具提示 <ListBox Name="FillSelections" VerticalContentAlignment="Stretch" Margin="1, 3, 1, 3" IsEnabled="True" Grid.Column="0" Background="Transparent" Horizo

我有一个
列表框
控件;如何使用下面的代码为每个
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>

试试这样的方法

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


当然,无论您的绑定源是什么,都要调整ItemsSource绑定,并且绑定路径将包含列表中实际要显示的对象的任何公共属性

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


当然,无论您的绑定源是什么,都要调整ItemsSource绑定,并且绑定路径将包含列表中实际要显示的对象的任何公共属性。

您可以为
ListBoxItem
创建样式。因此,大致如下:

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <ToolTip>
                        <TextBlock>Hello</TextBlock>
                    </ToolTip>
                </Setter.Value>
            </Setter>
        </Style>
   </Window.Resources>
    <Grid>
        <ListBox>
            <ListBoxItem>
                <TextBlock Text="Hello" />
            </ListBoxItem>
        </ListBox>
    </Grid>
</Window>

你好

您可以为
ListBoxItem
创建样式。因此,大致如下:

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <ToolTip>
                        <TextBlock>Hello</TextBlock>
                    </ToolTip>
                </Setter.Value>
            </Setter>
        </Style>
   </Window.Resources>
    <Grid>
        <ListBox>
            <ListBoxItem>
                <TextBlock Text="Hello" />
            </ListBoxItem>
        </ListBox>
    </Grid>
</Window>

你好

我确实尝试过,但唯一的问题是我无法更改我的数据模板,它必须位于我的listbox控件之外,请参阅我的listbox控件的图像--列表框中每个项目的工具提示我尝试过,但唯一的问题是我无法更改我的datatemplate,它必须位于我的listbox控件之外,查看我的listbox控件的图像--列表框中每个项目的工具提示我尝试过,但唯一的问题是我无法更改我的datatemplate,它必须位于我的listbox控件之外,请查看我的listbox控件的图像--列表框中每个项目的工具提示我不明白。如果添加样式,就不会更改数据模板。你可以把它放在任何你想放的地方,把它放在
而不是
中。我试过了,但唯一的问题是我不能更改我的数据模板,它必须在我的列表框控件之外,请看我的列表框控件的图像--列表框中每个项目的工具提示我不明白。如果添加样式,就不会更改数据模板。你可以把它放在任何你想放的地方,把它放在
而不是
中。