如何使用tooltip显示listviewitem或treeviewitem绑定的实体对象信息? RRS feed

  • 问题

  • 鼠标停留在listviewitem或treeviewitem上几秒钟,显示这个item绑定实体对象的相关信息,属性不为null的就显示,为null的就不显示。

    2017年3月6日 1:49


  • Hi 轮回的齿轮,

    我这边做了一个例子。你可以参考下。 我增加了一个转换器,来判定当前的值是否为null。


            <local:ToolTipVisibilitySetter x:Key="VisibilityConverter"/>
        <Grid Margin="0,0,6.293,90.897">
            <ListView Margin="10" Name="lvUsers">
                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="ToolTip">
                                <UniformGrid Columns="1">
                                    <TextBlock Text="{Binding Name}" Visibility="{Binding Name,Converter={StaticResource VisibilityConverter}}"/>
                                    <TextBlock Text="{Binding Age}" Visibility="{Binding Age,Converter={StaticResource VisibilityConverter}}"/>
                                    <TextBlock Text="{Binding Mail}" Visibility="{Binding Mail,Converter={StaticResource VisibilityConverter}}"/>
                        <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                        <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                        <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />


     public partial class Listviewwithgridview : Window
            public Listviewwithgridview()
                List<TestModels> items = new List<TestModels>();
                items.Add(new TestModels() { Name = "John Doe", Age = 42, Mail = "" });
                items.Add(new TestModels() { Name = null, Age = 39, Mail = "" });
                items.Add(new TestModels() { Name = "Sammy Doe", Age = 15, Mail = null});
                lvUsers.ItemsSource = items;
        class ToolTipVisibilitySetter : IValueConverter
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                if (value == null || value.ToString() == string.Empty)
                    return Visibility.Collapsed;
                    return Visibility.Visible;
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                throw new NotImplementedException();
        public class TestModels
            public string Name { get; set; }
            public int Age { get; set; }
            public string Mail { get; set; }

    Best Regards,

    Yohann Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    2017年3月6日 7:45