WPF(MultiBinding 数据对比验证,启用提交)

<Window x:Class="TestOfMultiBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <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,0" />
        <Button x:Name="button1" Content="Submit" 
                Width="80" Margin="5" />
    </StackPanel>
</Window>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace TestOfMultiBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent(); 
            this.SetMultiBinding();
        }

        private void SetMultiBinding()
        {
            Binding b1 = new Binding("Text")
                             {
                                 Source = this.textBox1
                             };

            Binding b2 = new Binding("Text")
                             {
                                 Source = this.textBox2
                             };

            Binding b3 = new Binding("Text")
                             {
                                 Source = this.textBox3
                             };

            Binding b4 = new Binding("Text")
                             {
                                 Source = this.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();

            this.button1.SetBinding(Button.IsEnabledProperty, mb);
        }  
    }

    public class LogonMultiBindingConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.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[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}


转载于:https://www.cnblogs.com/wjchang/archive/2013/04/09/3671535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值