<Window x:Class="WPF熊俊.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Name="window1" Height="399.259" Width="604.074">
    <Grid HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
        <ListBox ItemsSource="{Binding ElementName=window1, Path=ListPerson}" DisplayMemberPath="Name"  HorizontalAlignment="Left" Height="160" Margin="40,41,0,0" VerticalAlignment="Top" Width="108"/>
    </Grid>
</Window>public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ListPerson = new List<Person>() 
        {
            new Person(){Name="张三",Adress="南街"}, 
            new Person(){Name="李四",Adress="北京"},
            new Person(){Name="王二",Adress="上海"},
        };
    }
    private  List<Person> listperson;
    public List<Person> ListPerson
    {
        get { return listperson; }
        set { listperson = value; } 
    }
}public class Person
{
    public string Name { get; set; }
    public string Adress { get; set; }
}
上面的代码中,将窗口的Name属性设为window1,在XAML标签中,将ListBox的ItemsSource绑定到窗口,然后指定ListPerson属性,可是,结果为什么没有效果呢?哪里不正确呢?

解决方案 »

  1.   

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ListPerson = new List<Person>() 
            {
                new Person(){Name="张三",Adress="南街"}, 
                new Person(){Name="李四",Adress="北京"},
                new Person(){Name="王二",Adress="上海"},
            };
            this.DataContext = ListPerson;
        } 
        private  List<Person> listperson;
        public List<Person> ListPerson
        {
            get { return listperson; }
            set { listperson = value; } 
        }
    }
     
    public class Person
    {
        public string Name { get; set; }
        public string Adress { get; set; }
    }<Window x:Class="WPF熊俊.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Name="window1" Height="399.259" Width="604.074">
        <Grid HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
            <ListBox ItemsSource="{Binding}" DisplayMemberPath="Name"  HorizontalAlignment="Left" Height="160" Margin="40,41,0,0" VerticalAlignment="Top" Width="108"/>
        </Grid>
    </Window>
      

  2.   

    <Grid HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
            <ListBox Name="listBox1" ItemsSource="{Binding ElementName=window1, Path=ListPerson}" DisplayMemberPath="Name"  HorizontalAlignment="Left" Height="160" Margin="40,41,0,0" VerticalAlignment="Top" Width="108"/>
        </Grid>
     ListPerson = new List<Person>() 
                {
                    new Person(){Name="张三",Adress="南街"}, 
                    new Person(){Name="李四",Adress="北京"},
                    new Person(){Name="王二",Adress="上海"},
                };            this.listBox1.ItemsSource = ListPerson;//加这一句绑定即可显示
      

  3.   

    你没有绑定,当然没有显示了。更多信息参考:http://www.cnblogs.com/Health/archive/2012/02/17/2355924.html
      

  4.   

    XAML中的绑定,为什么不行
    xaml只是控件,没有绑定,后者说是只是指向了绑定的属性!
     this.listBox1.ItemsSource = ListPerson;才是绑定
      

  5.   

    XAML中的绑定,为什么不行
    xaml只是控件,没有绑定,后者说是只是指向了绑定的属性!
     this.listBox1.ItemsSource = ListPerson;才是绑定没有指定数据源嘛,绑定的数据源都没有指定,自然是绑定不了咯
      

  6.   

    因为你写的绑定是在 InitializeComponent()时候执行的,这时候ListPerson为空,所以没有显示任何数据。
    把初始化顺序换下就可以了:
    ListPerson = new List<Person>() {  };
    InitializeComponent();或者把ListPerson做成DependencyProperty,这样ListPerson变了以后才能通知ui作出反应。
      

  7.   


    这个才是真正回答了楼主的问题。我借此再说几句:
    1. 无特殊要求的话,用ViewModel来绑定。
    2. ListPerson——没有这么命名的,名称应该言简意赅,这里用People更合理。
      

  8.   

    不知道啊,抱什么错,有错误信息吗?
    我发现你有时候测试的内容都是改过的,用下面的代码测试下吧:<Window x:Class="WPF熊俊.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Name="window1" Height="399.259" Width="604.074">
        <Grid HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517">
            <ListBox ItemsSource="{Binding ElementName=window1, Path=ListPerson}" DisplayMemberPath="Name"  HorizontalAlignment="Left" Height="160" Margin="40,41,0,0" VerticalAlignment="Top" Width="108"/>
        </Grid>
    </Window>
    using System.Collections.Generic;
    using System.Windows;namespace WPF熊俊
    {
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    ListPerson = new List<Person>()
    {
    new Person() {Name = "张三", Adress = "南街"},
    new Person() {Name = "李四", Adress = "北京"},
    new Person() {Name = "王二", Adress = "上海"},
    };
    InitializeComponent();
    } private List<Person> listperson;
    public List<Person> ListPerson
    {
    get { return listperson; }
    set { listperson = value; }
    }
    } public class Person
    {
    public string Name { get; set; }
    public string Adress { get; set; }
    }
    }
      

  9.   

    DependencyProperty,要写成依赖属性的.
      

  10.   

     private DependencyProperty ListPersonProperty;
            public Window1()
            {
                ListPersonProperty = DependencyProperty.Register("ListPerson", typeof(List<Person>), typeof(Window1), new PropertyMetadata());
                InitializeComponent();
                this.ListPerson = new List<Person>() { new Person() { Name = "dasdada" } };
            }        public List<Person> ListPerson
            {
                get { return (List<Person>)GetValue(ListPersonProperty); }
                set
                {
                    SetValue(ListPersonProperty, value);
                }
            }
        }    public class Person
        {
            public string Name { get; set; }
        }
      

  11.   

    主要是附加属性的问题,我以前也遇见过,这个就是附加属性一般属性的区别,附加属性实例化在类实例化之前,一般属性在类实例化之后
    你这样绑定时 一般属性还没有实例化赋值,你可以使用一个Ob开头集合(可变集合),修改,你发现后面才会改变,
    这样使用
    看这个
    http://silverlightchina.net/html/tips/2011/0520/7769.html
    把这ListPerson变成附加属性就好了
      

  12.   

    XAML还要加
    DataContext="{Binding RelativeSource={RelativeSource Self}}“然后直接绑定 dataSoruce={bind 你注册的属性名字我上面的是(Source),你的是啥就是啥}
      

  13.   

    真是奇怪,回到家就正常显示了。
    顺便问下,启动程序之后,是前台先执行、还是后台先执行呢?为什么把InitializeComponent方法放到下面位置就可以了...
      

  14.   

    先执行后台程序。在InitializeComponent()方法中,用LoadComponent加载对应的xaml数据流:
    Application.LoadComponent(this, new Uri("/WpfApp1;component/mainwindow.xaml", UriKind.Relative));