<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属性,可是,结果为什么没有效果呢?哪里不正确呢?
{
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>
<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;//加这一句绑定即可显示
xaml只是控件,没有绑定,后者说是只是指向了绑定的属性!
this.listBox1.ItemsSource = ListPerson;才是绑定
xaml只是控件,没有绑定,后者说是只是指向了绑定的属性!
this.listBox1.ItemsSource = ListPerson;才是绑定没有指定数据源嘛,绑定的数据源都没有指定,自然是绑定不了咯
把初始化顺序换下就可以了:
ListPerson = new List<Person>() { };
InitializeComponent();或者把ListPerson做成DependencyProperty,这样ListPerson变了以后才能通知ui作出反应。
这个才是真正回答了楼主的问题。我借此再说几句:
1. 无特殊要求的话,用ViewModel来绑定。
2. ListPerson——没有这么命名的,名称应该言简意赅,这里用People更合理。
我发现你有时候测试的内容都是改过的,用下面的代码测试下吧:<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; }
}
}
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; }
}
你这样绑定时 一般属性还没有实例化赋值,你可以使用一个Ob开头集合(可变集合),修改,你发现后面才会改变,
这样使用
看这个
http://silverlightchina.net/html/tips/2011/0520/7769.html
把这ListPerson变成附加属性就好了
DataContext="{Binding RelativeSource={RelativeSource Self}}“然后直接绑定 dataSoruce={bind 你注册的属性名字我上面的是(Source),你的是啥就是啥}
顺便问下,启动程序之后,是前台先执行、还是后台先执行呢?为什么把InitializeComponent方法放到下面位置就可以了...
Application.LoadComponent(this, new Uri("/WpfApp1;component/mainwindow.xaml", UriKind.Relative));