在FlipView中显示绑定数据

问题描述

我已经在XAML中声明了FlipView控件,并且希望将其绑定到在(.cs文件)后面的代码中定义和填充的集合源。此外,texblock将显示集合中某项的特定属性

FlipView的XAML声明:

        <FlipView x:Name="FlipViewStudents" Grid.Row="1" Height="Auto" Width="Auto" ItemsSource="{x:Bind Students}">
            <FlipView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock x:Name="StudentName" Text="{Binding Name}" FontSize="60" Foreground="Green" FontWeight="Bold" TextAlignment="Left"/>
                    </StackPanel>
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>
收集变量的

C#代码隐藏(属于此XAML文件的.cs文件):

private List<SingleStudent> Students { get; set; }

更新: 我正在Page_Loaded事件处理程序中初始化collection变量:

Students = new List<SingleStudent>();

此外,我正在使用具有30秒刻度间隔的dispatcherTimer,并在dispatcherTimer_Tick(出现刻度时)处理程序内部填充了收集变量(首先将其清除后):

Students.Clear();
var singleStudent = new SingleStudent()
{
    Name = "John",Grade = "B" 
};    
Students.Add(singleStudent);

如图所示,每30秒将清除并填充一次收集变量。我希望每当将项目添加到集合中时,GUI中的FlipView都会自动更新。

我已经调试并检查了对象是否已通过代码添加到集合中,但是它在GUI中未显示任何内容

如图所示,SingleStudent是一个具有不同属性的类,包括Name(名称),该属性是要在FlipView中显示的项目属性

我也尝试过使用ObservableCollection,但这也不会显示任何内容。 帮助或提示表示赞赏。

解决方法

如前所述,在使用ListViewList<SingleStudent>时使用ObservableCollection<SingleStudent>而不是翻转视图时也会遇到相同的问题

我做了一些测试,结果发现在Page_Loaded事件中初始化集合会破坏您的数据绑定。因此,您应该定义:

private ObservableCollection<SingleStudent> Students = new ObservableCollection<SingleStudent>();

private ObservableCollection<SingleStudent> Students { get; set; }
public TestPage() //Constructor of your page
{
    Students = new ObservableCollection<SingleStudent>();
}

此外,您看到我使用的是ObservableCollection<SingleStudent>。您必须使用它而不是列表。使用List<SingleStudent>无效,因为添加项目不会提高INotifyPropertyChanged,因此不会更新数据绑定。