数据绑定类到Silverlight控件工具提示

时间:2012-08-08 10:29:31

标签: silverlight class xaml data-binding silverlight-4.0

拥有以下作者类

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

1 个答案:

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

希望这会有所帮助:)