我在GridView中显示数据时遇到问题。我似乎无法做到正确,我找不到任何其他相关主题。可能我正在监督一些非常简单的事情。
我有这个gridview:
<GridView ItemsSource="{Binding Source={StaticResource itemsViewSource}}" Width="383" Height="216"/>
<Button Content="Button" Click="Button_Click" />
我有这个CollectionViewSource:
<Page.Resources>
<CollectionViewSource x:Name="itemsViewSource" Source="{Binding NamesList, Mode=TwoWay}"/>
</Page.Resources>
本守则背后:
人类:
public class Person : INotifyPropertyChanged {
private string m_name;
private string m_gender;
public event PropertyChangedEventHandler PropertyChanged;
public string Name {
get {
return m_name;
}
set {
m_name = value;
RaisePropertyChanged("Name");
}
}
public string Gender {
get {
return m_gender;
}
set {
m_gender = value;
RaisePropertyChanged("Gender");
}
}
protected void RaisePropertyChanged(string PropertyName) {
var eventhandler = this.PropertyChanged;
if (eventhandler != null) {
eventhandler(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
网主页:
public sealed partial class MainPage : Page{
public ObservableCollection<Person> NamesList {get; set;}
public MainPage(){
this.InitializeComponent();
Person p = new Person();
Person p2 = new Person();
Person p3 = new Person();
p.Name = "Piet";
p.Gender = "Male";
p2.Name = "Pietin";
p2.Gender = "Female";
p3.Name = "Piet de tweede";
p3.Gender = "Male";
NamesList = new ObservableCollection<Person>();
NamesList.Add(p);
NamesList.Add(p2);
NamesList.Add(p3);
}
: private void Button_Click(object sender, RoutedEventArgs e) {
itemsViewSource.Source = NamesList;
}
}
为什么我的GridView没有显示任何数据或添加按钮点击数据?
@edit 16:16 - 15-09-2014 如果点击后我添加以下内容,则显示项目。但事实并非如此。
itemsViewSource.Source = NamesList;
@edit 09:04 - 16-09-2014 我添加了这个调试图像以显示绑定是好的,他可以找到NamesList。但是,如果我查看itemsViewSource,它说Source是null。
答案 0 :(得分:2)
由于您使用的是普通List<T>
,因此WPF无法知道何时添加或删除项目。
请尝试使用ObservableCollection<T>
。
编辑:
NamesList
需要成为财产。 WPF只能绑定到属性:
public sealed partial class MainPage : Page
{
public ObservableCollection<Person> NamesList { get; private set; }
public MainPage(){
this.InitializeComponent();
Person p = new Person();
Person p2 = new Person();
p.Name = "Piet";
p.Gender = "Male";
p2.Name = "Pietin";
p2.Gender = "Female";
NamesList = new ObservableCollection<Person>();
NamesList.Add(p);
NamesList.Add(p2);
}
答案 1 :(得分:0)
我找到了两个答案。
回答1:
只需将其添加到&lt;页面&gt;
DataContext="{Binding RelativeSource={RelativeSource Self}}
回答2:
我必须在Mainpage中设置DataContext并将其引用到Class(在我的例子中是Person类)(在我看来,它不合逻辑,并且设置以下代码行更容易:
itemsViewSource.Source = NamesList;
我做了以下改编;
的MainPage:
public sealed partial class MainPage : Page{
public MainPage(){
this.InitializeComponent();
DataContext = new Person();
}
}
Person Class(添加以下内容)(我必须将其视为数据类):
public ObservableCollection<Person> NamesList { get; set; }
public Person() {
Person p = new Person("Piet", "Male");
Person p2 = new Person("Pietin", "Female");
NamesList = new ObservableCollection<Person>();
NamesList.Add(p);
NamesList.Add(p2);
}
public Person(string Name, string Gender){
m_name = Name;
m_gender = Gender;
}
如果我这样设置,我可以正确使用CollectionViewSource的源绑定。 (之前源绑定为null。)
<CollectionViewSource x:Name="itemsViewSource" Source="{Binding NamesList, Mode=TwoWay}"/>