数据绑定(十一)多路绑定MultiBinding
有时候UI要显示的信息又不止一个数据来源决定,就需要使用MultiBinding,MultiBinding具有一个名为Bindings的属性,其类型是Collection<BindingBase>,通过这个属性MultiBinding把一组Binding对象聚合起来,处在这个集合中的Binding对象可以拥有自己的数据校验与转换机制,它们汇集起来的数据将共同决定传往MultiBinding目标的数据。
举例:有如下界面
<StackPanel Background="LightBlue">
<TextBox x:Name="textBox1" Height="23" Margin="5" />
<TextBox x:Name="textBox2" Height="23" Margin="5,0" />
<TextBox x:Name="textBox3" Height="23" Margin="5" />
<TextBox x:Name="textBox4" Height="23" Margin="5" />
<Button x:Name="button1" Content="Submit" Width="80" Margin="5" />
</StackPanel>
后台代码中设置MultiBinding
Binding b1 = new Binding("Text") { Source = textBox1 };
Binding b2 = new Binding("Text") { Source = textBox2 };
Binding b3 = new Binding("Text") { Source = textBox3 };
Binding b4 = new Binding("Text") { Source = textBox4 }; MultiBinding mb = new MultiBinding() { Mode = BindingMode.OneWay };
mb.Bindings.Add(b1);
mb.Bindings.Add(b2);
mb.Bindings.Add(b3);
mb.Bindings.Add(b4);
mb.Converter = new LogonMultiBindingConverter();
button1.SetBinding(Button.IsEnabledProperty, mb);
转换器由于需要对多个值进行转换,所以需要使用IMultiValueConverter的派生类进行转换,转换器代码
class LogonMultiBindingConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (!values.Cast<string>().Any(Text => string.IsNullOrEmpty(Text))
&& values[0].ToString() == values[1].ToString()
&& values[2].ToString() == values[3].ToString())
{
return true;
} return false;
} public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
运行效果如图
当第一行和第二行的内容相同,并且,第三行和第四行的内容相同时,Submit按钮可用,否则Submit不可用
数据绑定(十一)多路绑定MultiBinding的更多相关文章
- WPF多路绑定
WPF多路绑定 多路绑定实现对数据的计算,XAML: 引用资源所在位置 xmlns:cmlib="clr-namespace:CommonLib;assembly=CommonLib&q ...
- Kendo MVVM 数据绑定(十一) Value
Kendo MVVM 数据绑定(十一) Value Value 绑定可以把 ViewModel 的某个属性绑定到 DOM 元素或某个 UI 组件的 Value 属性.当用户修改 DOM 元素或 UI ...
- Binding(五):多路绑定
Binding不止能绑定一个源,它还能绑定多个源,这就是我们这节要讲的多路绑定:MultiBinding. 使用多路绑定跟一般的绑定还是有区别的,首先它并不能很好的在标记扩展中使用,另外,使用多路绑定 ...
- 【Angular 5】数据绑定、事件绑定和双向绑定
本文为Angular5的学习笔记,IDE使用Visual Studio Code,内容是关于数据绑定,包括Property Binding.Class Binding.Style Binding. 在 ...
- CPF 入门教程 - 数据绑定和命令绑定(二)
CPF netcore跨平台UI框架 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) 数据绑定和Wpf类似,支持双向绑定.数据绑定和命令绑定是UI和业务逻辑分离的基础 ...
- Vue之变量、数据绑定、事件绑定使用举例
vue1.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...
- android 数据绑定(6)自定义绑定方法、双向数据绑定
1.官方文档 https://developer.android.com/topic/libraries/data-binding/binding-adapters https://developer ...
- Kendo UI开发教程(22): Kendo MVVM 数据绑定(十一) Value
Value绑定可以把ViewModel的某个属性绑定到DOM元素或某个UI组件的Value属性.当用户修改DOM元素或UI组件的值时,绑定的ViewModel的值也随之发生改名.同样,如果ViewMo ...
- 数据绑定—Source(绑定到静态类的静态属性)
<UserControl x:Class="绑定.绑定Source" xmlns="http://schemas.microsoft.com/winfx/2006/ ...
随机推荐
- 【33.33%】【codeforces 552B】Vanya and Books
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Java 常用工具类---- 各种字符集编码判断与转换
import java.io.UnsupportedEncodingException; /** * 判断字符编码 * * @author guyinyihun */ public class Cha ...
- Net Core 实现谷歌翻译ApI 免费版
原文:Net Core 实现谷歌翻译ApI 免费版 由于谷歌翻译官方API是付费版本,本着免费和开源的精神.分享一下用 Net Core 实现谷歌翻译API的代码. 项目引用的Nuget 包: Cha ...
- .gitignore 设置忽略上传的文件
首先在一个项目中新建如下所示文件用来测试 image.png 一.生成.gitignore文件 1.进入项目根目录,打开终端: 2.输入 vi .gitignore 创建并打开隐藏文件.gitigno ...
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
TypeScript has 'interface' and 'type', so when to use which? interface hasName { firstName: string; ...
- [NPM] Pass arguments to npm scripts
Often times you’ll have variations that you’ll want to make to your npm scripts and repeating yourse ...
- 机器学习 Softmax classifier (一个隐含层)
程序实现 softmax classifier, 含有一个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...
- Architectures for concurrent graphics processing operations
BACKGROUND 1. Field The present invention generally relates to rendering two-dimension representatio ...
- 电子商务系统的设计与实现(十):DWZ框架与第三方分页组件整合
晚上,就是刚刚,在后端管理系统中使用DWZ框架. 先是,直接使用官网网站的Demo,dwz-jui,与编程语言无关的纯静态的那个原始项目. 很快就搭建好了左侧菜单,打开菜单后,出现Tab页面,然后显示 ...
- Fastjson 序列化,反序列化Map对象排序问题(字符串转map,map转字符串)
背景 记录项目中遇到的 关于fastjson jsonobject转string乱序,string转jsonObject乱序问题的解决方案 fastJson issues 问题来源描述参见: http ...