前段:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid> <Grid.RowDefinitions> <RowDefinition Height="140"/> <RowDefinition Height="150"/> <RowDefinition Height="140"/> <RowDefinition Height="100*"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0"> <TextBlock Width="248" Height="24" Text="股票名称:" TextWrapping="Wrap"/> <ListBox x:Name="listStockName" Width="248" Height="56"> <ListBoxItem Content="全通教育"/> <ListBoxItem Content="大智慧"/> <ListBoxItem Content="宝钢股份"/> <ListBoxItem Content="浦发银行"/> <ListBoxItem Content="工商银行"/> <ListBoxItem Content="中国建筑"/> <ListBoxItem Content="中国南车"/> </ListBox> <TextBlock Width="248" Height="24" Text="你所选中的股票名称:" /> <TextBlock Width="248" Height="24" Text="{Binding ElementName=listStockName, Path=SelectedItem.Content}"> </TextBlock> </StackPanel> <StackPanel Grid.Row="1"> <TextBlock Width="248" Height="24" Text="颜色:" TextWrapping="Wrap"/> <ListBox x:Name="listColor" Width="248" Height="56"> <ListBoxItem Content="Blue"/> <ListBoxItem Content="Red"/> <ListBoxItem Content="Green"/> <ListBoxItem Content="Gray"/> <ListBoxItem Content="Cyan"/> <ListBoxItem Content="GreenYellow"/> <ListBoxItem Content="Orange"/> </ListBox> <TextBlock Width="248" Height="24" Text="改变背景色:" /> <TextBlock Width="248" Height="24" Text="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}" Background="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}"> </TextBlock> <TextBox Name="txtTwoWay" Text="{Binding ElementName=listColor,Path=SelectedItem.Content,Mode=TwoWay}" Background="{Binding ElementName=listColor,Path=SelectedItem.Content,Mode=TwoWay}"></TextBox> </StackPanel> <StackPanel Grid.Row="2"> <StackPanel.Resources> <XmlDataProvider x:Key="MyColors" Source="Colors.xml" XPath="colors"> </XmlDataProvider> </StackPanel.Resources> <TextBlock Width="248" Height="24" Text="XML数据绑定:" TextWrapping="Wrap"/> <ListBox x:Name="listXmlColor" Width="248" Height="56" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource MyColors},XPath=color/@name}"> </ListBox> <TextBlock Width="248" Height="24" Text="选中的颜色:" /> <TextBlock Width="248" Height="24" Text="{Binding ElementName=listXmlColor, Path=SelectedValue, Mode=OneWay}"> </TextBlock> </StackPanel> <StackPanel Grid.Row="3"> <StackPanel.Resources> <ObjectDataProvider x:Key="students" ObjectType="{x:Type local:StudentService}" MethodName="GetStudentList"> </ObjectDataProvider> <DataTemplate x:Key="studentLayout" DataType="students"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="Blue"/> <TextBlock Text=", "></TextBlock> <TextBlock Text="{Binding Path=Age}"></TextBlock> <TextBlock Text=", "></TextBlock> <TextBlock Text="{Binding Path=Birthday}"></TextBlock> <TextBlock Text=", "></TextBlock> <TextBlock Text="{Binding Path=Country}"></TextBlock> </StackPanel> </DataTemplate> <CollectionViewSource x:Key="studentsView" Source="{Binding Source={StaticResource students}}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="Name" Direction="Ascending" /> <scm:SortDescription PropertyName="Age" Direction="Descending" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource> </StackPanel.Resources> <TextBlock Width="248" Height="24" Text="对象数据绑定:" TextWrapping="Wrap"/> <ListBox x:Name="listObjectBind" Width="450" Height="80" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource students}}" ItemTemplate="{DynamicResource studentLayout}"> </ListBox> <TextBlock Width="248" Height="24" Text="数据排序:" TextWrapping="Wrap"/> <DataGrid DataContext="{StaticResource studentsView}" AutoGenerateColumns="False" ItemsSource="{Binding}" CanUserAddRows="False"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Name}" Header="名称" /> <DataGridTextColumn Binding="{Binding Age}" Header="年龄" /> <DataGridTextColumn Binding="{Binding Country}" Header="国家" /> <DataGridTextColumn Binding="{Binding Birthday}" Header="出生日期" /> </DataGrid.Columns> </DataGrid> </StackPanel>
<Button Content="Button" HorizontalAlignment="Left" Margin="24,96,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.179,-0.938" ContentStringFormat="http://dmsite.chinacloudsites.cn/root/index.html" Click="Button_Click"/> </Grid> </Window>

后端

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} } public class StudentService
{ public List<Student> GetStudentList()
{ Student liang = new Student(); liang.Age = ""; liang.Name = "梁丘"; liang.Birthday = "1990-02-03"; liang.Country = "中国"; Student zuo = new Student(); zuo.Age = ""; zuo.Name = "左丘"; zuo.Birthday = "1992-02-03"; zuo.Country = "中国"; Student diwu = new Student(); diwu.Age = ""; diwu.Name = "第五言"; diwu.Birthday = "1982-11-03"; diwu.Country = "中国"; Student yang = new Student(); yang.Age = ""; yang.Name = "羊舌微"; yang.Birthday = "2002-11-13"; yang.Country = "中国"; List<Student> personList = new List<Student>(); personList.Add(liang); personList.Add(zuo); personList.Add(diwu); personList.Add(yang); return personList; } } public class Student : DependencyObject
{ //声明一个静态只读的DependencyProperty字段 public static readonly DependencyProperty NameProperty; public static readonly DependencyProperty AgeProperty; public static readonly DependencyProperty BirthdayProperty; public static readonly DependencyProperty CountryProperty; static Student()
{ //注册我们定义的依赖属性Name,Age,birthday,Country NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student), new PropertyMetadata("名称", OnValueChanged)); AgeProperty = DependencyProperty.Register("Age", typeof(string), typeof(Student), new PropertyMetadata("年龄", OnValueChanged)); BirthdayProperty = DependencyProperty.Register("Birthday", typeof(string), typeof(Student), new PropertyMetadata("出生日期", OnValueChanged)); CountryProperty = DependencyProperty.Register("Country", typeof(string), typeof(Student), new PropertyMetadata("国家", OnValueChanged)); } private static void OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{ //当值改变时,我们可以在此做一些逻辑处理 } //属性包装器,通过它来读取和设置我们刚才注册的依赖属性 public string Name
{ get { return (string)GetValue(NameProperty); } set { SetValue(NameProperty, value); } } public string Age
{ get { return (string)GetValue(AgeProperty); } set { SetValue(AgeProperty, value); } } public string Birthday
{ get { return (string)GetValue(BirthdayProperty); } set { SetValue(BirthdayProperty, value); } } public string Country
{ get { return (string)GetValue(CountryProperty); } set { SetValue(CountryProperty, value); } } }
}

wpf数据绑定的更多相关文章

  1. WPF 数据绑定Binding

    什么是数据绑定? Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简单而一致的方法来显示数据以及与数据交互. 通过数据绑定,您可以对两个不同对象 ...

  2. WPF数据绑定Binding(二)

    WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...

  3. WPF——数据绑定(一)什么是数据绑定

    注意:本人初学WPF,文中可能有表达或者技术性问题,欢迎指正!谢谢! 一:什么是数据绑定? “Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简 ...

  4. 剖析WPF数据绑定机制

    引言 WPF框架采取的是MVVM模式,也就是数据驱动UI,UI控件(Controls)被严格地限制在表示层内,不会参与业务逻辑的处理,只是通过数据绑定(Data Binding)简单忠实地表达与之绑定 ...

  5. WPF 10天修炼 第十天- WPF数据绑定

    WPF数据绑定 数据绑定到元素属性是将源对象指定为一个WPF元素,并且源属性是一个依赖属性,依赖属性内置了变更通知.当改变源对象依赖属性值之后,绑定目标可以立即得到更新,开发人员不需要手动编写响应事件 ...

  6. 微软原文翻译:适用于.Net Core的WPF数据绑定概述

    原文链接,大部分是机器翻译,仅做了小部分修改.英.中文对照,看不懂的看英文. Data binding overview in WPF 2019/09/19 Data binding in Windo ...

  7. C#-WPF数据绑定基础(一)

    前言:WPF数据绑定技术有效的提高了程序的容错率,可以最大程度的保持程序的健壮性,从而降低程序在使用过程中崩掉的可能性. 接下来,我将分享一下我在写测量程序过程中所用到的数据绑定方面的知识 首先,我所 ...

  8. C#WPF数据绑定模板化操作四步走

    前言:WPF数据绑定对于WPF应用程序来说尤为重要,本文将讲述使用MVVM模式进行数据绑定的四步走用法: 具体实例代码如下: 以下代码仅供参考,如有问题请在评论区留言,谢谢 1 第一步:声明一个类用来 ...

  9. WPF 数据绑定 1_1 基础知识&绑定到元素属性

    A.数据绑定基础: 数据源对象:WPF将从该对象中提取信息,交由目标对象进行显示. 目标对象:从数据源中提取信息,并赋给该对象的属性. B.绑定到元素属性 最简单的绑定情形则是将一个源对象指定为一个W ...

  10. WPF 数据绑定基础

    纯理论,可能会枯燥. .net 技术群: 199281001 ,欢迎加入. 1.目标对象一定是派生自DependencyObject的对象,并且目标属性必须是依赖属性,否则数据绑定操作将会失   败. ...

随机推荐

  1. java多线程系类:基础篇:08之join

    本章,会对Thread中join()方法进行介绍.涉及到的内容包括:1. join()介绍2. join()源码分析(基于JDK1.7.0_40)3. join()示例 转载请注明出处:http:// ...

  2. vue 滚动加载

    <template> <div class="wraper" @scroll="onScroll($event)"> <div c ...

  3. redis 学习笔记(2)-client端示例代码

    redis提供了几乎所有主流语言的client,java中主要使用二种:Jedis与Redisson 一.Jedis的使用 <dependency> <groupId>redi ...

  4. hadoop: hbase1.0.1.1 伪分布安装

    环境:hadoop 2.6.0 + hbase 1.0.1.1 + mac OS X yosemite 10.10.3 安装步骤: 一.下载解压 到官网 http://hbase.apache.org ...

  5. Silverlight调用本机exe程序

    要点: 1. Silverlight必须启用OOB模式,以及 Require elevated trust when running in-browser.参考下图设置 注:OOB模式,并不意味着必须 ...

  6. TinyFrame升级之九:实现复杂的查询

    本章我们主要讲解如何实现一个复杂的查询.由于目前TinyFrame框架已经投入到了实际的项目生产中,所以我很乐意将项目中遇到的任何问题做以记录并备忘. 这章中,我们提到的查询界面如下所示: 其中,涉及 ...

  7. [MetaHook] Find a function signature

    Find a non-public function signature, we need a tool "IDA Pro" ( You can open picture in a ...

  8. SQLServer(MSSQL)、MySQL、SQLite、Access相互迁移转换工具 DB2DB v1.3

    最近公司有一个项目,需要把原来的系统从 MSSQL 升迁到阿里云RDS(MySQL)上面.为便于测试,所以需要把原来系统的所有数据表以及测试数据转换到 MySQL 上面.在百度上找了很多方法,有通过微 ...

  9. JS事件详解

    hello,我是沐晴,最近呢,来总结一下 JS中的常用的事件,希望我们都能一起查漏补缺. 焦点事件 焦点事件在表单中经常用到,那什么是焦点呢?比如我们文本框里面的有光标的时候,就是获得了焦点,我们就可 ...

  10. 在win8(win8.1)电脑上安装IIS,配置web服务器,发布网站

    1.IIS安装: 打开控制面板——程序和功能——启用或关闭Windows功能——找到(Windows功能下)下的(Internet Infornation Services)把Web 管理工具和万维网 ...