C# 数据绑定集合ViewSource和GridView
在GridView中显示数据时出现问题。我似乎无法正确理解,也找不到其他与此相关的话题。也许我在监督一些非常简单的事情 我得到了这个gridview:C# 数据绑定集合ViewSource和GridView,c#,xaml,gridview,windows-runtime,C#,Xaml,Gridview,Windows Runtime,在GridView中显示数据时出现问题。我似乎无法正确理解,也找不到其他与此相关的话题。也许我在监督一些非常简单的事情 我得到了这个gridview: <GridView ItemsSource="{Binding Source={StaticResource itemsViewSource}}" Width="383" Height="216"/> <Button Content="Button" Click="Button_Click" /> 主页: public
<GridView ItemsSource="{Binding Source={StaticResource itemsViewSource}}" Width="383" Height="216"/>
<Button Content="Button" Click="Button_Click" />
主页:
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;
}
}
@编辑09:04-16-09-2014
我添加了这个调试图像以显示绑定良好,他可以找到名称列表。但是如果我看一下itemsViewSource,它会说源是空的。
因为您使用的是纯文本,所以WPF无法知道何时添加或删除项目 试着用一个新的 编辑:
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);
}
公共密封部分类主页面:第页
{
公共ObservableCollection名称列表{get;private set;}
公共主页(){
this.InitializeComponent();
人员p=新人员();
人员p2=新人员();
p、 Name=“Piet”;
p、 性别=“男性”;
p2.Name=“Pietin”;
p2.性别=“女性”;
名称列表=新的ObservableCollection();
名称列表。添加(p);
名称列表。添加(p2);
}
我找到了两个答案
答案1:
只需将其添加到DataContext="{Binding RelativeSource={RelativeSource Self}}
答案2:
我必须在主页中设置DataContext并将其引用到一个类(在我的例子中是Person类)(我认为这不符合逻辑,设置以下代码行更容易:
itemsViewSource.Source = NamesList;
我做了以下的改编
主页:
public sealed partial class MainPage : Page{
public MainPage(){
this.InitializeComponent();
DataContext = new Person();
}
}
Person类(添加了以下内容)(我必须将其更多地视为一个数据类):
publicobservableCollection名称列表{get;set;}
公众人士(){
人员p=新人员(“皮特”、“男性”);
人员p2=新人员(“Pietin”、“女性”);
名称列表=新的ObservableCollection();
名称列表。添加(p);
名称列表。添加(p2);
}
公众人物(字符串名称、字符串性别){
m_name=名称;
m_性别=性别;
}
如果我像这样设置它,我就能够正确使用CollectionViewSource的源绑定。(以前源绑定为null。)
我刚刚将其更改为InotifyProperty已更改的ObservableCollection,我将编辑该问题。名称列表
是一个字段而不是属性。WPF只能绑定到属性。我将其转换为属性,但它仍然不显示。(它是Windows 8.1应用商店应用程序)为什么当我将代码中的源代码设置为名称列表时,它会工作?(例如,在我的按钮单击事件中)
DataContext="{Binding RelativeSource={RelativeSource Self}}
itemsViewSource.Source = NamesList;
public sealed partial class MainPage : Page{
public MainPage(){
this.InitializeComponent();
DataContext = new Person();
}
}
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 x:Name="itemsViewSource" Source="{Binding NamesList, Mode=TwoWay}"/>