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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12430835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:21:52  来源:igfitidea点击:

How to set a ToolTip for each ListBoxItem

wpflistboxtooltipitemtemplate

提问by user1535848

I have a ListBoxcontrol; how would I set the ToolTipfor each ListBoxItemusing the code below.

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

回答by Cybermaxs

Try something like this

尝试这样的事情

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

Of course adapt the ItemsSource binding with whatever your binding source is, and the binding Path parts with whatever public property of the objects in the list you actually want to display.

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

回答by Tejas Sharma

You could create a style for ListBoxItem. So something along the lines of:

您可以为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>