拥有以下作者类
Public Class Author
{
Public String Name{get;set;}
Public String Description{get;set;}
Public int NumberOfBooks{get;set;}
}
并通过DataTemplate将List<Author>
数据绑定到Listbox,如下所示
<ListBox ItemsSource="{Binding Authors}" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel ToolTipService.ToolTip="{Binding Description}" ToolTipService.Placement="Right">
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如何使用 Xaml 描述性声明式绑定生成工具提示,如下所示
AuthorName ( NumberOfBooks)
Description
答案 0 :(得分:1)
你可以简单。
<TextBlock Text="{Binding Name}">
<ToolTipService.ToolTip>
<ToolTip>
<StackPanel>
<TextBlock>
<Run Text="{Binding AuthorName}"/>
<Run Text=" "/>
<Run Text="{Binding NumberOfBooks}"/>
</TextBlock>
<TextBlock MaxWidth="150"
Text="{Binding Description}" TextWrapping="Wrap"/>
</StackPanel>
</ToolTip>
</ToolTipService.ToolTip>
</TextBlock>
或者说得好看。
<TextBlock Text="{Binding Name}">
<ToolTipService.ToolTip>
<ToolTip>
<StackPanel>
<Border Background="CadetBlue" Margin="5" Padding="5">
<TextBlock Text="{Binding AuthorName}" HorizontalAlignment="Center"/>
</Border>
<TextBlock Text="{Binding NumberOfBooks, StringFormat='No. of Books: \{0\}'}"/>
<TextBlock MaxWidth="150"
Text="{Binding Description}" TextWrapping="Wrap"/>
</StackPanel>
</ToolTip>
</ToolTipService.ToolTip>
</TextBlock>
希望这会有所帮助:)