listView与gridView使用类似,这里讲解gridView的一些数据绑定(x:Bind)基础知识。

顺便学习下如何使用属性通知。(后台中的数据变化会直接显示在UI上,实现动态变化,默认是没有属性通知的)

首先需要声明一个类,添加一些属性。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace XBind.Models
  8. {
  9. public class Worker:INotifyPropertyChanged//继承属性通知接口
  10. {
  11. //Demo中,只实现Years的属性通知
  12. private string name;
  13. public string Name
  14. {
  15. get { return name; }
  16. set {
  17. name = value;
  18. NotifyPropertyChanged("Name");
  19. }
  20. }
  21. private string age;
  22. public string Age
  23. {
  24. get { return age; }
  25. set
  26. {
  27. age = value;
  28. NotifyPropertyChanged("Age");
  29. }
  30. }
  31. private string years;
  32. public string Work_Years
  33. {
  34. get => years;
  35. set
  36. {
  37. years = value;
  38. NotifyPropertyChanged("Work_Years");
  39. }
  40. }
  41. //实现属性通知
  42. public event PropertyChangedEventHandler PropertyChanged;
  43. public void NotifyPropertyChanged(string propertyName)
  44. {
  45. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  46. }
  47. }
  48. }

Xaml代码

  1. <Page
  2. x:Class="XBind.MainPage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:XBind"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. xmlns:models="using:XBind.Models"
  9. mc:Ignorable="d"
  10. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  11. <Grid>
  12. <Grid.ColumnDefinitions>
  13. <ColumnDefinition Width="Auto"/>
  14. <ColumnDefinition/>
  15. </Grid.ColumnDefinitions>
  16. <StackPanel Orientation="Vertical" VerticalAlignment="Center">
  17. <StackPanel Orientation="Vertical">
  18. <TextBlock Text="Add Worker" FontSize="30" FontWeight="Bold"/>
  19. <TextBox x:Name="name_textBox" PlaceholderText="Name"/>
  20. <TextBox x:Name="age_textBox" PlaceholderText="Age" Margin="0,5"/>
  21. <TextBox x:Name="years_textBox" PlaceholderText="Years"/>
  22. <Button x:Name="add_bt" Content="Add" Tapped="add_bt_Tapped" Margin="0,10,0,0"/>
  23. </StackPanel>
  24. <StackPanel Orientation="Vertical" Margin="0,20,0,0">
  25. <TextBlock Text="Search by name,and change working years" FontSize="30" FontWeight="Bold"/>
  26. <TextBox x:Name="search_textBox" PlaceholderText="Enter name" Margin="0,0,0,5"/>
  27. <TextBox x:Name="newYears_textBox" PlaceholderText="Enter new working years"/>
  28. <Button x:Name="change_bt" Content="OK" Tapped="change_bt_Tapped" Margin="0,10,0,0"/>
  29. </StackPanel>
  30. </StackPanel>
  31. <GridView x:Name="gird_view" Grid.Column="1" ItemsSource="{x:Bind workers}">
  32. <GridView.ItemContainerStyle>
  33. <Style TargetType="GridViewItem">
  34. <Setter Property="Margin" Value="10"/>
  35. </Style>
  36. </GridView.ItemContainerStyle>
  37. <GridView.ItemTemplate>
  38. <DataTemplate x:DataType="models:Worker">
  39. <StackPanel Orientation="Vertical" Width="100" BorderBrush="SkyBlue" BorderThickness="1">
  40. <TextBlock Text="{x:Bind Name}"/>
  41. <TextBlock Text="{x:Bind Age}"/>
  42. <TextBlock Text="{x:Bind Work_Years,Mode=OneWay}"/>
  43. </StackPanel>
  44. </DataTemplate>
  45. </GridView.ItemTemplate>
  46. </GridView>
  47. </Grid>
  48. </Page>

这里注意的是,因为只实现了Years的属性通知更改, Text="{x:Bind Years,Mode=OneWay}",Mode默认或OneTime都不会更改。

后台代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using Windows.Foundation;
  8. using Windows.Foundation.Collections;
  9. using Windows.UI.Popups;
  10. using Windows.UI.Xaml;
  11. using Windows.UI.Xaml.Controls;
  12. using Windows.UI.Xaml.Controls.Primitives;
  13. using Windows.UI.Xaml.Data;
  14. using Windows.UI.Xaml.Input;
  15. using Windows.UI.Xaml.Media;
  16. using Windows.UI.Xaml.Navigation;
  17. using XBind.Models;
  18. // https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板
  19. namespace XBind
  20. {
  21. /// <summary>
  22. /// 可用于自身或导航至 Frame 内部的空白页。
  23. /// </summary>
  24. public sealed partial class MainPage : Page
  25. {
  26. private ObservableCollection<Worker> workers;//之所以使用ObservableCollection而不是List,因为前者支持UI动态变化
  27. public MainPage()
  28. {
  29. this.InitializeComponent();
  30. workers = new ObservableCollection<Worker>();
  31. }
  32. private void add_bt_Tapped(object sender, TappedRoutedEventArgs e)
  33. {
  34. var work = new Worker
  35. {
  36. Name = name_textBox.Text,
  37. Age = age_textBox.Text,
  38. Work_Years = years_textBox.Text
  39. };
  40. workers.Add(work);
  41. }
  42. private async void change_bt_Tapped(object sender, TappedRoutedEventArgs e)
  43. {
  44. if (workers.Count > 0 )
  45. {
  46. foreach (var item in workers)
  47. {
  48. if (item.Name.ToLower() == search_textBox.Text.ToLower())
  49. {
  50. item.Work_Years = newYears_textBox.Text;
  51. }
  52. else
  53. {
  54. var message = new MessageDialog("Check no such person!");
  55. await message.ShowAsync();
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }

Demo源码:https://github.com/singhwong/uwp-xBind-demo.git

uwp,c#,listView与gridView列表控件进阶的更多相关文章

  1. UWP开发必备:常用数据列表控件汇总比较

    今天是想通过实例将UWP开发常用的数据列表做汇总比较,作为以后项目开发参考.UWP开发必备知识点总结请参照[UWP开发必备以及常用知识点总结]. 本次主要讨论以下控件: GridView:用于显示数据 ...

  2. WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: Dat ...

  3. 【转】WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: DataGrid自定义样式: ListView自定义样式: 二.Dat ...

  4. 【Android】12.0 UI开发(三)——列表控件ListView的简单实现2

    1.0 由于书上内容,已经和实际编程的兼容性已经不太友好,重写了项目,用于进一步学习列表控件ListView. 2.0 新建项目ListViewTest,其中文件目录如下: 3.0 ActivityC ...

  5. .NET各大平台数据列表控件绑定原理及比较(WebForm、Winform、WPF)

    说说WebForm: 数据列表控件: WebForm 下的列表绑定控件基本就是GridView.DataList.Repeater:当然还有其它DropDownList.ListBox等. 它们的共同 ...

  6. Windows Phone 8.1 列表控件(2):分组数据

    说到 List 控件,Windows Phone 8.1 上推荐使用的是 ListView 和 GridView. 而这两个控件实在太多东西可讲了,于是分成三篇来讲: (1)基本 (2)分组数据 (3 ...

  7. WPF: 实现带全选复选框的列表控件

    本文将说明如何创建一个带全选复选框的列表控件.其效果如下图:     这个控件是由一个复选框(CheckBox)与一个 ListView 组合而成.它的操作逻辑: 当选中“全选”时,列表中所有的项目都 ...

  8. Github上star数超1000的Android列表控件

    Android开发中,列表估计是最最常使用到的控件之一了.列表相关的交互如下拉刷新,上拉更多,滑动菜单,拖动排序,滑动菜单,sticky header分组,FAB等等都是十分常见的体验.Github中 ...

  9. 【Android】15.0 UI开发(六)——列表控件RecyclerView的网格布局排列实现

    1.0 列表控件RecyclerView的网格布局排列实现,关键词GridLayoutManager. LinearLayoutManager 实现顺序布局 GridLayoutManager 实现网 ...

随机推荐

  1. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  2. surprise库官方文档分析(一)

    一:入门 1.基本用法 (1).自动交叉验证 Surprise有一套内置的 算法和数据集供您使用.在最简单的形式中,只需几行代码即可运行交叉验证程序: from surprise import SVD ...

  3. 开源是个巨大的坑,谁来帮帮我 - smartmontools 虐我记

    最近在试用smartmontools,感觉还行,于是乎想找来源码改改试试,这下可好,掉坑里了.呜呜呜... smartmontools的源码在这里可以看到:https://www.smartmonto ...

  4. 欢迎使用CSDN的markdown编辑器

    以下是蒻鞫第一次打开CSDN-markdown编译器的温馨提示,感觉CSDN好贴心,不作任何用途,仅为纪念,若存在违法侵权行为,请联系留言,立即删除. List item 这里写 欢迎使用Markdo ...

  5. Tkinter 之pack布局

    一参数说明 参数 作用 anchor 控制组件在 pack 分配的空间中的位置"n", "ne", "e", "se", ...

  6. 关于bootstrap table 固定列宽

    首先为table 设置 style="table-layout: fixed;" <table id="assessStage" data-height= ...

  7. SCHED_FIFO与SCHED_OTHER调度机制

    疑问 两个线程分别有不同的调度策略,一个SCHED_FIFO,一个SCHED_OTHER,按照之前的理解,SCHED_FIFO实时线程一定会占用CPU一直运行,导致SCHED_OTHER的普通线程得不 ...

  8. Ubuntu 16.04安装完重启后黑屏,光标一直闪

    原文:https://blog.csdn.net/weixin_38533896/article/details/81023690 版权声明:本文为博主原创文章,转载请附上博文链接! 安装教程网址:h ...

  9. cv相关博客文章

    收藏些图像处理,机器学习,深度学习方面比较不错的文章,时常学习,复习和膜拜吧... 图像方面(传统CV): 1. SIFT特征 https://www.cnblogs.com/wangguchangq ...

  10. 唯品会HDFS性能挑战和优化实践

    唯品会HDFS性能挑战和优化实践 原创: 大数据平台 唯技术 4月1日 https://mp.weixin.qq.com/s/LMa99ubgACI4eaDV3G-6gw