实现WPF界面控件属性与后台数据属性绑定。

  建立解决方案如下:

    

    

MainWindow添加

  一个ListView,显示List,添加绑定语句:  ItemsSource="{Binding Test}“。

  两个Button,增加List和清空List。

xaml代码如下:

 1 <Window x:Class="TESTBind.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="350" Width="525">
 5     <Grid>
 6         <ListView Height="100" HorizontalAlignment="Left" Margin="12,39,0,0" Name="listView1" VerticalAlignment="Top" Width="481"
 7                   ItemsSource="{Binding Test}"/>
 8         <Button Content="List++" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
 9         <Button Content="ClearList" Height="23" HorizontalAlignment="Left" Margin="124,12,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
10     </Grid>
11 </Window>

View Code

添加BindModel,定义需要绑定的变量

添加Microsoft.Practices.Prism.dll库,引用using Microsoft.Practices.Prism.ViewModel;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.ViewModel;namespace TESTBind.Models
{public class BindModel : NotificationObject{public BindModel(){test = new ObservableCollection<string>();}private ObservableCollection<string> test;public ObservableCollection<string> Test{get { return test; }set{test = value;this.RaisePropertyChanged("Test");}}}
}

View Code

为Test赋值,bindModel.Test = new ObservableCollection<string>(testList);

这里ObservableCollection提供数据更新,我之前用过List,但是只能更新一次,当绑定值第二次改变时,程序就会报错。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Data;
 8 using System.Windows.Documents;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using TESTBind.Models;
15 using System.Collections.ObjectModel;
16
17 namespace TESTBind
18 {
19     /// <summary>
20     /// MainWindow.xaml 的交互逻辑
21     /// </summary>
22     public partial class MainWindow : Window
23     {
24         public MainWindow()
25         {
26             InitializeComponent();
27             Init();
28         }
29         private void Init()
30         {
31             listNum = 1;
32             testList = new List<string>();
33             bindModel = new BindModel();
34             this.DataContext = bindModel;
35         }
36         private void button1_Click(object sender, RoutedEventArgs e)
37         {
38             testList.Add("WPF界面绑定" + listNum);
39             bindModel.Test = new ObservableCollection<string>(testList);
40             listNum++;
41         }
42         private int listNum;
43         private List<string> testList;
44         private BindModel bindModel;
45
46         private void button2_Click(object sender, RoutedEventArgs e)
47         {
48             testList.Clear();
49             bindModel.Test = new ObservableCollection<string>(testList);
50             listNum = 1;
51         }
52     }
53 }

View Code

下载链接:http://pan.baidu.com/s/1cgcVKy

转载于:https://www.cnblogs.com/jwxjjh/p/5700619.html

WPF入门(一)——绑定Binding相关推荐

  1. WPF入门教程系列(二) 深入剖析WPF Binding的使用方法

    同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProperty)只能拥有一个binding. 这一点可以通过设置bindi ...

  2. WPF入门知识(学习)

    WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知识(或者其他.NET支持的语言):这个是当然的了,虽 ...

  3. WPF入门教程(七)---依赖属性(3)(转)

    WPF入门教程(七)---依赖属性(3) 2018年08月24日 08:33:43 weixin_38029882 阅读数:50 四. 只读依赖属性 在以前在对于非WPF的功能来说,对于类的属性的封装 ...

  4. WPF中的Data Binding调试指南

    点击蓝字"大白技术控"关注我哟 加个"星标★",每日良时,好文必达! WPF中的Data Binding如何Debug? 大家平时做WPF开发,相信用Visua ...

  5. WPF入门教程系列(一) 创建你的第一个WPF项目

    WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知识(或者其他.NET支持的语言):这个是当然的了,虽 ...

  6. WPF 入门教程(一)

    WPF 入门教程(一) 1.布局规则 1.WPF 窗体中,一个窗体只能持有一个空间,当需要展示多个控件时,则需要首先设置一个容器控件(Container).控件的布局有容器来决定. 2.控件应避免明确 ...

  7. WPF入门教程(八)--依赖属性(4)(转)

    WPF入门教程(八)--依赖属性(4) 2018年08月27日 11:35:55 weixin_38029882 阅读数:71 我们通过下面的这幅图,简单介绍一下WPF属性系统对依赖属性操作的基本步骤 ...

  8. WPF入门:数据绑定

    原文:WPF入门:数据绑定 上一篇我们将XAML大概做了个了解 ,这篇将继续学习WPF数据绑定的相关内容 数据源与控件的Binding Binding作为数据传送UI的通道,通过INotityProp ...

  9. 【WPF】动态设置Binding的ConverterParameter转换器参数

    原文:[WPF]动态设置Binding的ConverterParameter转换器参数 问题:XAML中,想要在一个Bingding语句中再次Bingding. Source="{Bindi ...

  10. WPF入门教程系列三——Application介绍(续)

    接上文WPF入门教程系列二--Application介绍,我们继续来学习Application 三.WPF应用程序的关闭 WPF应用程序的关闭只有在应用程序的 Shutdown 方法被调用时,应用程序 ...

最新文章

  1. Linux qgis 编译,QGIS简介与源代码编译
  2. VMWARE安装LINUX
  3. Windows2012使用笔记
  4. 如何判断数据背离正态分布?
  5. Docker入门与实战
  6. java数据流无法输出验证码
  7. PostgreSQL数据保留窗口功能的使用
  8. 计算机中英文打字文章,中英文混合打字文章
  9. Qt Creator启动慢的解决方法
  10. ndows 资源管理器,windows资源管理器已停止工作怎么解决
  11. H3C交换机对接思科交换机
  12. 如果你的团队有这7个特性,那么你的团队就会战无不胜
  13. 计算机学术为啥分要比专硕低那么多,专硕,凭什么是鄙视链最底端的一群人
  14. 用SRS搭建流媒体系统
  15. 20190918爱奇艺2020校招题
  16. 2021年R1快开门式压力容器操作考试及R1快开门式压力容器操作考试资料
  17. ISO14443 Type B类型卡的防碰撞过程以及命令解析
  18. android光度传感器开发,Android开发之光线传感器用法
  19. Linux操作系统--文本编辑器(保姆级教程)
  20. conan入门(五):conan 交叉编译引用第三方库示例

热门文章

  1. Boost:扩展分配器的测试程序
  2. Boost:使用静态c ++内核语言扩展以进行编译和 执行模板化的c ++内核
  3. Boost:基于Boost的一个微小的actor框架
  4. ITK:使用FFT与输入图像的掩码图像进行归一化相关
  5. ITK:两个图像的平方差
  6. ITK:创建一个点集
  7. VTK:Utilities之TimeStamp
  8. VTK:图片之ImageRange3D
  9. VTK:图片之ImageStencil
  10. OpenCV Viz转变