WPF中列表框项的自定义工具提示

问题描述 投票:2回答:3

我有一个ListBox,显示一个ObalentableCollection的Talent对象。当用户将鼠标悬停在ListBox中的每个项目上时,我想在ToolTip中显示有关Talent的几条信息。

我的ListBox:

<ListBox ItemsSource="{Binding ElementName=CE_Races_racesLB, Path=SelectedItem.Talents}" ItemTemplate="{StaticResource removableTalentListTemplate}" />

ItemTemplate:

<DataTemplate x:Key="removableTalentListTemplate">
    <StackPanel>
        <TextBlock FontSize="13" Text="{Binding Path=tName}" VerticalAlignment="Center" Width="175" Height="18" Grid.Column="0" />
    </StackPanel>
</DataTemplate>

如果我将TextTipService.ToolTip =“{Binding Path = Description”添加到TextBlock属性,我可以显示Talent的描述。但是,当我尝试创建这样的自定义工具提示时:

<DataTemplate x:Key="removableTalentListTemplate">
    <StackPanel>
        <TextBlock FontSize="13" Text="{Binding Path=tName}" VerticalAlignment="Center" Width="175" Height="18" Grid.Column="0" />
            <TextBlock.ToolTip>
                <ToolTip>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Description}" />
                    </StackPanel>
                </ToolTip>
            </TextBlock.ToolTip>
        </TextBlock>
    </StackPanel>
</DataTemplate>

当我转到鼠标悬停ListBox项时,工具提示只是说“System.Windows.Controls.StackPanel”。我真的想创建一个很好的工具提示,显示很多信息,但我无法通过这个障碍。这是现在的截图:http://silkforge.com/dev/ss.jpg。您无法看到鼠标,但您可以在ListBox项目“Acute Hearing I”下看到工具提示。

wpf xaml listbox tooltip
3个回答
5
投票

你试过这个吗?

<TextBlock Text="{Binding Path=tName}">
  <ToolTipService.ToolTip>
    <StackPanel>
      <TextBlock Text="{Binding Path=Description}" />
    </StackPanel>
  </ToolTipService.ToolTip>
</TextBlock>

2
投票

经过快速测试后,您的代码似乎运行正常。

我的测试:

XAML:

 <Window x:Class="WpfApplication4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication4"
            xmlns:Controls="clr-namespace:Liz.Common"
           Title="MainWindow" Height="300" Width="400" Name="UI" >

 <Window.Resources>
    <DataTemplate x:Key="removableTalentListTemplate">
            <StackPanel>
                <TextBlock FontSize="13" Text="{Binding Path=tName}" VerticalAlignment="Center" Width="175" Height="18" Grid.Column="0" >
                    <TextBlock.ToolTip>
                        <ToolTip Background="Blue">
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Description}" Foreground="White" />
                            </StackPanel>
                        </ToolTip>
                    </TextBlock.ToolTip>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
     </Window.Resources>

        <Grid>
            <ListBox ItemTemplate="{StaticResource removableTalentListTemplate}" ItemsSource="{Binding ElementName=UI, Path=Models}" />
        </Grid>
    </Window>

码:

public partial class MainWindow : Window
{
    private ObservableCollection<MyModel> _models = new ObservableCollection<MyModel>();

    public MainWindow()
    {
        InitializeComponent();
        Models.Add(new MyModel { tName = "Test", Description = "Hello" });
    }

    public ObservableCollection<MyModel> Models
    {
        get { return _models; }
        set { _models = value; }
    }
}

public  class MyModel : INotifyPropertyChanged
{
    private string _tName;
    private string _description;

    public string tName 
    {
        get { return _tName; }
        set { _tName = value; NotifyPropertyChanged("tName"); } 
    }

    public string Description
    {
        get { return _description; }
        set { _description = value; NotifyPropertyChanged("Description"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

结果:


0
投票

我不知道我是否遇到过与OP相同的问题,但对我来说麻烦的来源是以下隐含的风格:

<Style TargetType="{x:Type ToolTip}">
   <Setter Property="ContentTemplate">
       <Setter.Value>
           <DataTemplate>
               <TextBlock TextWrapping="Wrap" Text="{Binding}" MaxWidth="600"/>
           </DataTemplate>
       </Setter.Value>
   </Setter>
</Style>

这种风格的目的是消除超出可见屏幕区域的冗长工具提示,但它仅适用于标准的内联工具提示。尝试创建自定义,精心设计的工具提示导致OP报告的“System.Windows.Controls.StackPanel”。删除此隐式样式立即解决了问题...但是也删除了漂亮的600 DIP宽度限制。

© www.soinside.com 2019 - 2024. All rights reserved.