C# GridView数据绑定问题
我在GridView数据绑定方面遇到问题 我已经在ViewModelGV.cs文件中创建了一个视图模型C# GridView数据绑定问题,c#,xaml,data-binding,windows-8,C#,Xaml,Data Binding,Windows 8,我在GridView数据绑定方面遇到问题 我已经在ViewModelGV.cs文件中创建了一个视图模型 class ViewModelGV { public ObservableCollection<Person> Persons{ get; set; } public ViewModelGV() { Persons = new ObservableCollection<Person>();
class ViewModelGV
{
public ObservableCollection<Person> Persons{ get; set; }
public ViewModelGV()
{
Persons = new ObservableCollection<Person>();
Person person1 = new Person();
person1.Name = "John";
Persons.Add(person1);
Person person2 = new Person();
person2.Name = "Jack";
Persons.Add(person2);
Person person3 = new Person();
person3.Name = "Stephen";
Persons.Add(person3);
}
}
然后在我的XAML文件中,我将DataContext指向了我的ViewModelGV(我添加了一些问号来标记代码中的要点)
并添加了CollectionViewSource
<Page.Resources>
<CollectionViewSource x:Name="Src" IsSourceGrouped="False" ItemsPath="?" Source="{Binding Persons}" />
</Page.Resources>
我不知道如何指向特定的itemname值,它可能与ItemsPath属性有关
然后在我的GridView中,我想创建一个StackPanel,下面是矩形和TextBlock
<Grid x:Name="LayoutGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<GridView ItemsSource="{Binding Source={StaticResource Src}}">
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<StackPanel>
<Rectangle Fill="Red" Width="100" Height="100" />
<TextBlock Text="{Binding ? Name ? }" FontSize="40" />
</StackPanel>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
<TextBlock Text="{Binding Persons.Count}" Margin="0,200,0,0"></TextBlock>
</Grid>
以下是显示正确计数但未显示模板和人员数据的结果
您是否尝试过:
<TextBlock Text="{Binding Person.Name}" FontSize="40" />
我最近也做了类似的事情,并让它发挥了作用。尝试将xaml更改为以下内容
<GridView ItemsSource="{Binding Path=Persons, Mode=TwoWay}">
及
我没有使用集合视图源,只是直接绑定到视图模型中的可观察集合。希望这有帮助
<TextBlock Text="{Binding Person.Name}" FontSize="40" />
<GridView ItemsSource="{Binding Path=Persons, Mode=TwoWay}">
<TextBlock Text="{Binding Name}" FontSize="40"/>