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的对象,并且目标属性必须是依赖属性,否则数据绑定操作将会失 败. ...
随机推荐
- codevs1910 递归函数
难度等级:黄金 codevs1910 递归函数 题目描述 Description 对于一个递归函数w(a, b, c). 如果a <= 0 or b <= 0 or c <= 0就返 ...
- Python2.4-原理之函数
此节来自于<Python学习手册第四版>第四部分 一.函数基础 函数的作用在每个编程语言中都是大同小异的,,这个表是函数的相关语句和表达式. 1.编写函数,a.def是可执行代码,pyth ...
- Hadoop简单安装配置
Hadoop开始设计以Linux平台为运行目标,所以这里推荐在Linux发行版比如Ubuntu进行安装,目前已经有Hadoop for Windows出来,大家自行搜下文章. Hadoop运行模式分为 ...
- hystrix-turbine 监控的使用
1. 概述 Demo地址:http://git.oschina.net/zhou666/spring-cloud-7simple/tree/master/cloud-hystrix-turbine ...
- [译]用AngularJS构建大型ASP.NET单页应用(二)
原文地址:http://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single 客 ...
- Bootstrap系列 -- 42. 导航条中的按钮、文本和链接
Bootstrap框架的导航条中除了使用navbar-brand中的a元素和navbar-nav的ul和navbar-form之外,还可以使用其他元素.框架提供了三种其他样式: 1.导航条中的按钮na ...
- Linux 安装mysql+apache+php
安装mysql 1. yum install mysql mysql-server 2. 修改mysql密码 >use mysql >update user set passwor ...
- oracle-day1
今天的学习内容是oracle产品的三种安装方式,还有使用dbca静默建库 oracle产品的三种安装方式分别为: 1.图形化(Java向导)安装引导 2.使用应答文件静默安装 3.直接将装好的orac ...
- Hibernate断网修改配置文件实现正常验证运行
hibernate.cfg.xml中声明部分: <!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Conf ...
- iOS开发--录音简单实现