WPF入门(一)——绑定Binding

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

添加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");
            }
        }
    }
}

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

时间: 2024-08-07 17:55:51

WPF入门(一)——绑定Binding的相关文章

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

WPF入门教程系列(二) 深入剖析WPF Binding的使用方法 同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProperty)只能拥有一个binding. 这一点可以通过设置binding对象的方法名得知: public static BindingExpressionBase SetBinding( DependencyObject target, DependencyProperty dp, BindingB

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

WPF入门教程系列(一) 创建你的第一个WPF项目 WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知识(或者其他.NET支持的语言):这个是当然的了,虽然WPF是XAML配置的,但是总还是要写代码的,相信各位读者应该也都有这个基础了. 2) HTML语言:虽然WPF是窗体程序但是由于使用的XAML语言,如果以前接触过HTML.XHTML.ASP.NET之路的东西的话会,接受这些标签会很有帮助的,如

WPF元素的绑定

一.两个元素的简单绑     WPF元素的绑定,是指将两个控件绑在一起,比如利用滑动条Slider,改变刻度时,相应的标签元素中的字体的大小就会增加. 这种元素的绑定,要知道谁是目标元素,谁是源元素.比如用滑动条的刻度大小去绑定标签元素中的字体的大小.这里面滑动条的刻度就是源元素,标签字体大小就是目标元素. 在标签元素中字体大小的属性中就可以去直接绑定,绑定语句是: FontSize="{Binding ElementName=slider1,Path=Value} 解释:Binding Ele

[WPF]如何调试Data Binding

前言 在WPF开发中,将ViewModel中对象绑定到UI上时,会出现明明已经将数据对象Binding到UI,但是UI上就是不显示等等的问题.这篇博客将介绍WPF Data Binding调试相关的内容. 场景一(Binding的属性不存在) ViewModel: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new ViewMo

WPF之Menu绑定XML

一.XML文件 <?xml version="1.0" encoding="utf-8" ?> <MenuData xmlns=""> <Operation Name="文件" Gesture="F"> <Operation Name="新建" Gesture="N"> <Operation Name="

[转载]WPF入门教程系列一——基础

一. 前言   最近在学习WPF,学习WPF首先上的是微软的MSDN,然后再搜索了一下网络有关WPF的学习资料.为了温故而知新把学习过程记录下来,以备后查.这篇主要讲WPF的开发基础,介绍了如何使用Visual Studio 2013创建一个WPF应用程序. 首先说一下学习WPF的基础知识: 1) 要会一门.NET所支持的编程语言.例如C#. 2) 会一点“标准通用标记语言”:WPF窗体程序使用的XAML语言,也属于“标准通用标记语言”的一个分支.如果以前接触过XML.HTML.XHTML.AS

WPF入门教程系列二——Application介绍

原文:WPF入门教程系列二--Application介绍 一.Application介绍 WPF和WinForm 很相似, WPF与WinForm一样有一个 Application对象来进行一些全局的行为和操作,并且每个 Domain (应用程序域)中仅且只有一个 Application 实例存在.和 WinForm 不同的是WPF Application默认由两部分组成 : App.xaml 和 App.xaml.cs,这有点类似于 Asp.Net WebForm,将定义和行为代码相分离. 微

WPF使用异步+绑定的方式处理大数据量

WPF的优势在于界面处理,即使是这样,在面对大数据量的时候也免不了界面假死,同一个线程里处理界面跟大数据量,这是不可避免的.解决办法还是有的,可以使用分页显示,虚拟加载,增加条件限制... 比较好的解决办法是使用异步+绑定的方式,即绑定控件的数据源,异步获取数据.要解决界面假死,异步获取数据是很容易想到的,但是即使这样,获取到数据之后再设置控件的数据源,这又是一个耗时的过程,所以需要绑定.如果有ViewModel(前提是实现了INotifiPropertyChanged)那就更好了,直接设置属性

WPF入门教程:HelloWord及布局

WPF入门教程:HelloWord及布局 参考资料:https://www.cnblogs.com/dotnet261010/p/6275821.html 1. 简介 1.1 什么是WPF? WPF:Windows Presentation Foundation,是微软推出的基于Windows Vista的用户界面框架.属于.NET Framework 3.0的一部分. 1.2 WPF特点 统一的编程模型:提供统一的控件.语音.视频.文档3D等技术: 与分辨率无关:基于矢量绘图,能够支持各种分辨