繁体   English   中英

Windows Phone 8.1数据绑定ListView [XAML]

[英]Windows Phone 8.1 Data Binding ListView [XAML]

C# :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        public class data
        {
            public string id { get; set; }
        }

        List<data> datalist = new List<data>();

        int counter = 100000000;

        private void button_Click(object sender, RoutedEventArgs e)
        {
            data add = new data();
            counter += 1;
            add.id = counter.ToString();
            datalist.Add(add);
        }
    }

XAML:

<Page
    x:Class="SocialApp1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SocialApp1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Button x:Name="button" 
                Content="Get" HorizontalAlignment="Left" Margin="67,550,0,0" VerticalAlignment="Top" Click="button_Click" Width="368"/>
        <ListView Margin="24,0,19,311">
            <ListView.ItemTemplate>
              <DataTemplate>
                <Border CornerRadius="20" Background="DodgerBlue" Height="65">
                  <StackPanel Orientation="Horizontal">
                    <TextBlock TextWrapping="Wrap" Margin="10" Text="{Binding id}" FontSize="20" />
                  </StackPanel>
                </Border>
              </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>

嗨,我一直在尝试在Windows Phone 8.1中进行数据绑定。 我发布了所有代码,以防万一我丢失了一些东西。 我如何使其工作? 因为当我将其部署到手机上并按“ Get按钮时,它不会执行任何操作。

所需输出:

1)每次用户按下“ Get按钮时,都增加id

2)在datalist添加新的id

3)呈现在ListView中。

要更新ListView中的项目,必须通知绑定属性已更改。 在这种情况下,您具有要向其添加项目的集合,但是没有通知ListView集合已更改。 要通知ListView集合已更改,您必须使用ObservableCollection<T>或从其派生的类。 这意味着您必须将List<data>更改为ObservableCollection<data>

ObservableCollection<data> dataList { get; set; }

如果要将自定义方法/属性添加到集合中,或者使它更容易通过XAML声明实例,则可能需要创建ObservableCollection<T>的子类,以避免以后进行重构。 为什么要创建子类而不直接使用ObservableCollection<T> ,请参见此问题的答案: 从ObservableCollection继承的Collection-有什么好处?

如果以后遇到类似的问题,例如,更改集合的项时ListView不变,则应查看INotifyPropertyChanged -interface。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM