2013-12-09 146 views
0

我在GridView数据绑定中遇到了麻烦。GridView数据绑定问题

我在ViewModelGV.cs文件中创建了一个View Model。

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); 
    } 
} 

而另一类具有简单Name属性的人。

class Person 
{ 
    private String _Name; 

    public String Name 
    { 
     get { return _Name; } 
     set { _Name = value; } 
    } 
} 

然后在我的XAML文件中我已经指出的DataContext我ViewModelGV(我增加了一些问号标记代码要点)。

<Page.DataContext> 
    <ViewModels:ViewModelGV/> 
</Page.DataContext> 

,并添加CollectionViewSource

<Page.Resources> 
    <CollectionViewSource x:Name="Src" IsSourceGrouped="False" ItemsPath="?" Source="{Binding Persons}" /> 
</Page.Resources> 

我不知道怎么点的具体项目名称值,则可能有事情做与ItemsPath属性。

然后在我的GridView中,我想创建一个带有Rectangle和TextBlock的StackPanel。

<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> 

这里是显示正确的计数,但unproper模板和个人数据的结果

Result

回答

0

你试过:

<TextBlock Text="{Binding Person.Name}" FontSize="40" /> 
0

我已经做了类似的事情最近,把它加工。试着改变你的XAML以下

<GridView ItemsSource="{Binding Path=Persons, Mode=TwoWay}"> 

<TextBlock Text="{Binding Name}" FontSize="40"/> 

我没有使用集合查看源代码,只是直接绑定到可观察集合我的视图模型内。希望这可以帮助。