原文:数据绑定(十一)多路绑定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的更多相关文章

  1. WPF多路绑定

    WPF多路绑定 多路绑定实现对数据的计算,XAML:   引用资源所在位置 xmlns:cmlib="clr-namespace:CommonLib;assembly=CommonLib&q ...

  2. Kendo MVVM 数据绑定(十一) Value

    Kendo MVVM 数据绑定(十一) Value Value 绑定可以把 ViewModel 的某个属性绑定到 DOM 元素或某个 UI 组件的 Value 属性.当用户修改 DOM 元素或 UI ...

  3. Binding(五):多路绑定

    Binding不止能绑定一个源,它还能绑定多个源,这就是我们这节要讲的多路绑定:MultiBinding. 使用多路绑定跟一般的绑定还是有区别的,首先它并不能很好的在标记扩展中使用,另外,使用多路绑定 ...

  4. 【Angular 5】数据绑定、事件绑定和双向绑定

    本文为Angular5的学习笔记,IDE使用Visual Studio Code,内容是关于数据绑定,包括Property Binding.Class Binding.Style Binding. 在 ...

  5. CPF 入门教程 - 数据绑定和命令绑定(二)

    CPF netcore跨平台UI框架 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) 数据绑定和Wpf类似,支持双向绑定.数据绑定和命令绑定是UI和业务逻辑分离的基础 ...

  6. Vue之变量、数据绑定、事件绑定使用举例

    vue1.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...

  7. android 数据绑定(6)自定义绑定方法、双向数据绑定

    1.官方文档 https://developer.android.com/topic/libraries/data-binding/binding-adapters https://developer ...

  8. Kendo UI开发教程(22): Kendo MVVM 数据绑定(十一) Value

    Value绑定可以把ViewModel的某个属性绑定到DOM元素或某个UI组件的Value属性.当用户修改DOM元素或UI组件的值时,绑定的ViewModel的值也随之发生改名.同样,如果ViewMo ...

  9. 数据绑定—Source(绑定到静态类的静态属性)

    <UserControl x:Class="绑定.绑定Source" xmlns="http://schemas.microsoft.com/winfx/2006/ ...

随机推荐

  1. hibernate框架简单步骤

    Demo.java package com.itheima.test; import org.hibernate.Session; import org.hibernate.SessionFactor ...

  2. Windows 查看硬盘ID(diskpart命令)

    使用diskpart命令Win+R键运行cmd,进入命令行界面:1.diskpart2.list disk 查看有几块硬盘3.select disk 0 选择第一块硬盘4.detail disk 显示 ...

  3. 数组filter方法对数组元素进行过滤

    Array.prototype.filter对数组中元素进行过滤 /** * @method reduce * @param {number} item 当前迭代的数组元素 * @param {num ...

  4. 【poj1442】Black Box

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10890   Accepted: 4446 Description Our ...

  5. Linux下iptables屏蔽IP和端口号

    http://blog.csdn.net/kobejayandy/article/details/24332597 iptables 屏蔽端口

  6. [TypeScript] Create a fluent API using TypeScript classes

    You can create an easy to chain API using TypeScript classes. Learn about the thisreturn type annota ...

  7. 王立平--TableLayout

    效果: <?xml version="1.0" encoding="utf-8"?>  <TableLayout xmlns:android= ...

  8. AndroidMainifest标签使用说明3——&lt;activity-alias&gt;

    格式: <activity-alias android:enabled=["true" | "false"] android:exported=[&quo ...

  9. leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)

    题目例如以下: Given an input string, reverse the string word by word. For example, Given s = "the sky ...

  10. 【严肃脸】使用caffe实现色情图片的识别

    前言 前几天看到了雅虎开源了一个色情图片的识别模型新闻,上Github一看,是基于caffe的.试了试,模型效果很赞.Github地址:https://github.com/yahoo/open_ns ...