• 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();
            }
        }
    }
    


  • 相关阅读:
    文学、哲学段子
    文学、哲学段子
    js技术要点---JS 获取网页源代码
    泛型类,泛型方法,泛型委托的定义方法
    数组元素的逆序数
    stm32 ARM中的RO、RW和ZI DATA
    poj 3040 Allowance 贪心
    schedule()函数的调用时机(周期性调度)
    以JTextPanel为例Swing的鼠标事件详解
    实习生面试总结
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671535.html
Copyright © 2020-2023  润新知