开发者

WPF DisplayMemeberPath on ListBox

I'm having a problem with wpf listbox. my problem is that when I add items to listbox using listbox items and set the display memeber path nothing is showing. I want to use ListBoxItem or somthing similar to explicitly set tooltip for each item.

Thanks in Advance.

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            listbox1.DisplayMemberPath = "S";

            List<ListBoxItem> l = new List<ListBoxItem>();
            l.Add(new ListBoxItem() { Content = new Test() { S = "test1", I = 1 } });
            l.Add(new ListBoxItem() { Content = new Test() { S = "test2", I = 2 } });
            l.Add(new ListBoxItem() { Content = new 开发者_运维问答Test() { S = "test3", I = 3 } });
            l.Add(new ListBoxItem() { Content = new Test() { S = "test4", I = 4 } });
            foreach (var item in l)
            {
                listbox1.Items.Add(item);
            }

        }
    }

    public class Test
    {
        public string S { get; set; }
        public int I { get; set; }
    }
}


You're adding ListBoxItems to your ListBox, and ListBoxItem doesn't have a property named "S", so DisplayMemberPath won't work.

Add your custom classes directly:

listBox.Items.Add(new Test { S = "Hello World" });

You generally don't need to use ListBoxItems directly in WPF - just bind your ListBox directly to a custom collection and the ListBoxItems will be generated for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜