wpf数据绑定
前段:
<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数据绑定的更多相关文章
- WPF 数据绑定Binding
什么是数据绑定? Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简单而一致的方法来显示数据以及与数据交互. 通过数据绑定,您可以对两个不同对象 ...
- WPF数据绑定Binding(二)
WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...
- WPF——数据绑定(一)什么是数据绑定
注意:本人初学WPF,文中可能有表达或者技术性问题,欢迎指正!谢谢! 一:什么是数据绑定? “Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简 ...
- 剖析WPF数据绑定机制
引言 WPF框架采取的是MVVM模式,也就是数据驱动UI,UI控件(Controls)被严格地限制在表示层内,不会参与业务逻辑的处理,只是通过数据绑定(Data Binding)简单忠实地表达与之绑定 ...
- WPF 10天修炼 第十天- WPF数据绑定
WPF数据绑定 数据绑定到元素属性是将源对象指定为一个WPF元素,并且源属性是一个依赖属性,依赖属性内置了变更通知.当改变源对象依赖属性值之后,绑定目标可以立即得到更新,开发人员不需要手动编写响应事件 ...
- 微软原文翻译:适用于.Net Core的WPF数据绑定概述
原文链接,大部分是机器翻译,仅做了小部分修改.英.中文对照,看不懂的看英文. Data binding overview in WPF 2019/09/19 Data binding in Windo ...
- C#-WPF数据绑定基础(一)
前言:WPF数据绑定技术有效的提高了程序的容错率,可以最大程度的保持程序的健壮性,从而降低程序在使用过程中崩掉的可能性. 接下来,我将分享一下我在写测量程序过程中所用到的数据绑定方面的知识 首先,我所 ...
- C#WPF数据绑定模板化操作四步走
前言:WPF数据绑定对于WPF应用程序来说尤为重要,本文将讲述使用MVVM模式进行数据绑定的四步走用法: 具体实例代码如下: 以下代码仅供参考,如有问题请在评论区留言,谢谢 1 第一步:声明一个类用来 ...
- WPF 数据绑定 1_1 基础知识&绑定到元素属性
A.数据绑定基础: 数据源对象:WPF将从该对象中提取信息,交由目标对象进行显示. 目标对象:从数据源中提取信息,并赋给该对象的属性. B.绑定到元素属性 最简单的绑定情形则是将一个源对象指定为一个W ...
- WPF 数据绑定基础
纯理论,可能会枯燥. .net 技术群: 199281001 ,欢迎加入. 1.目标对象一定是派生自DependencyObject的对象,并且目标属性必须是依赖属性,否则数据绑定操作将会失 败. ...
随机推荐
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
- tomcat 新手上路
前提:本机先安装好JDK,保证常规java环境已经具备 1.下载Tomcat 7.0现在官网上好象已经没有安装程序版了,只有免解压zip版本(现在最新的版本是7.0.42) 下载地址 http://t ...
- Openwrt iptables分析
这里将载有Openwrt的WR841N的路由表dump出来分析一下. 这个是dump出iptables的命令 root@OpenWrt:/etc/config# iptables-save 这里分为4 ...
- 文件夹管理工具(MVC+zTree+layer)(附源码)
写在前 之前写了一篇关于 文件夹与文件的操作的文章 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite ) 把常用的对于文件与文件夹的操作总结了一 ...
- [poj 3537]Crosses and Crosses(博弈论)
题目:http://poj.org/problem?id=3537 题意:给你n个格子,两个人依次在n个格子的任意空位置画"X",谁如果画了一个后,3个X连在了一起,那么那个人就获 ...
- PHP-- 三种数据库随机查询语句写法
1. Oracle,随机查询查询语句-20条 select * from ( select * from 表名 order by dbms_random.value ) where rownum ...
- 52-which 显示系统命令所在目录
显示系统命令所在目录 which command-list 参数 command-list 是which搜索的一条或多条命令(实用程序) 示例 which 单条命令 $ which ls /bin/l ...
- 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型:解决方法
http://blog.csdn.net/xiaochongchong1248/archive/2009/11/20/4841193.aspx?1271573283 编程环境要求:VS2008/FX2 ...
- 十天冲刺---Day5
站立式会议 站立式会议内容总结: 燃尽图 照片 PM确实不应该交给组内编码最强的人来做. 编码的过程还要考虑整个项目的流程压力较大. 需要队友的支持和沟通.
- hibernate用setResultTransformer转换
当你用hibernate查出数据,但是类型不是原来的类型怎么办,新增的实体类还可以用,query.setResultTransformer(Transformers.aliasToBean(AA.cl ...