博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binding的源与路径
阅读量:7228 次
发布时间:2019-06-29

本文共 4622 字,大约阅读时间需要 15 分钟。

1.把控件作为Binding的源

例子:拖动Slider,输入框中的值也会跟着改变,或在输入框中输入数值,滑动条也会自动移动

View Code

 

 2.控制Binding的方向

设置属性:Mode

3.更新时操作

 设置属性:UpdateSourceTrigger

4.Path路径

1)前台与后台的代码比较:

前台:

View Code

 

后台:

using System;using System.Collections.Generic;using System.Linq;using System.Text;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 Path路径{    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            Binding binding = new Binding() { Path = new PropertyPath("Value"), Source = this.slider1 };            this.textbox2.SetBinding(TextBox.TextProperty, binding);            this.textBox6.SetBinding(TextBox.TextProperty, new Binding("Text.Length") { Source = this.textBox3, Mode = BindingMode.OneWay });            this.textBox9.SetBinding(TextBox.TextProperty, new Binding("Text[3]") { Source = this.textBox7, Mode = BindingMode.OneWay });        }    }}
View Code

 

截图:

2)斜线语法

第一个例子:

XAML:

View Code

 

C#:

using System;using System.Collections.Generic;using System.Linq;using System.Text;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.Shapes;namespace Path路径{    ///     /// 当使用一个集合或者DataView作为Binding的源时,如果想把它的默认元素当作Path使用,需要以下的语法    ///     public partial class Window1 : Window    {        public Window1()        {            InitializeComponent();        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            List
stringList = new List
() { "Tomi", "Tim", "Blogog" }; this.textBox1.SetBinding(TextBox.TextProperty, new Binding("/") { Source = stringList }); this.textBox2.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = stringList, Mode = BindingMode.OneWay }); this.textBox3.SetBinding(TextBox.TextProperty, new Binding("/[2]") { Source = stringList, Mode = BindingMode.OneWay }); } }}
View Code

 

截图:

第二个例子:

XAML:

View Code

 

C#:

using System;using System.Collections.Generic;using System.Linq;using System.Text;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.Shapes;namespace Path路径{    ///     /// 如果集合元素的属性仍然是一个集合时,使用多斜线语法    ///     public partial class Window2 : Window    {        public Window2()        {            InitializeComponent();        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            List
countryList = new List
(){ new Country(){ Name = "中国", ProvinceList = new List
(){ new Province(){ Name = "四川", CityList = new List
(){ new City(){ Name = "成都" } } } } } }; this.textBox1.SetBinding(TextBox.TextProperty, new Binding("/Name") { Source = countryList }); this.textBox2.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList/Name") { Source = countryList }); this.textBox3.SetBinding(TextBox.TextProperty, new Binding("ProvinceList/CityList/Name") { Source = countryList }); } }}
View Code

 

截图:

 

 

 

 

转载地址:http://hwdfm.baihongyu.com/

你可能感兴趣的文章
面试技术题笔记
查看>>
Myth源码解析系列之一-项目简介
查看>>
JS易混淆的方法整理
查看>>
iOS下JS与OC互相调用(八)--Cordova详解+实战
查看>>
七牛实时音视频云视频连线demo(web部分)
查看>>
Netty源码分析(六):SelectedSelectionKeySetSelector
查看>>
forEach,for...of,map与asycn/await
查看>>
springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)
查看>>
Golang数据库编程之GORM模型定义与数据库迁移
查看>>
Oracle redo解析之-4、rowid的计算
查看>>
Easy Scheduler 1.0.3 发布,分布式工作流任务调度系统
查看>>
java 颠倒整数
查看>>
Python入门教程100天:Day05-练习总结
查看>>
环境搭建,8种基本类型,Static,package和import,log4j
查看>>
即将到来的 Debian 10 Buster 发布版的新特点
查看>>
iOS 头部视图下拉变大
查看>>
Disruptor并发框架
查看>>
react-hooks 实现简单的评论list
查看>>
【多图警告】学会JavaScript测试你就是同行中最亮的仔(妹)
查看>>
19-04-25
查看>>