其他分享
首页 > 其他分享> > WPF UI元素间的Binding

WPF UI元素间的Binding

作者:互联网

WPF UI元素间的Binding

WPF的Binding起到桥梁的作用,UI元素之间的绑定,即binding的源是UI元素,目标也是UI元素,然后用Binding架起元素与元素属性(Property)之间的桥梁,最近学习深入浅出WPF这本书,整理了以下学习内容,和大家分享。

1. UI元素之间的绑定(直接绑定)

直接将TexrBox属性关联在Slider的Value属性上

      	<Window x:Class="WpfApp9.MainWindow"                              
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DirectBinding" Height="110" Width="300">
    <StackPanel>
        <TextBox x:Name="textBox1" Text="{Binding Path=Value,ElementName=slider1}" BorderBrush="Black" Margin="5"/>
        <Slider x:Name="slider1" Maximum="100" Minimum="0" Margin="5"/>
    </StackPanel>
	</Window>

    

2. UI元素之间的绑定(转换器)

将TexrBox是否为空绑定为按钮是否可用,即textBox为空,按钮不可用,textBox有输入内容,按钮button可用

  1. 添加转换器StringToEnableConverter类,此类需要继承IValueConverter,并实现Convert和ConvertBack方法。
     public class StringToEnableConverter : IValueConverter
    {
        // 将String转换为是否可用的Bool类型,为空则按钮不可用,否则为可用
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string str = (string)value;
            if (!string.IsNullOrEmpty(str))
            {
                return true;
            }
            return false;
        }
        // 反向转换器,暂时用不到
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
  2. 在前台添加引用对上步骤中声明转换器的命名空间的引用,见代码xmlns:local="clr-namespace:WpfApp9",
    然后添加Window.Resources中具体转换器的引用并添加key值,见代码<local:StringToEnableConverter x:Key="ste"/>,
    最后在Button的IsEnabled属性中进行Binding,见代码IsEnabled="}"
      <Window x:Class="WpfApp9.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp9"
        Title="ConverterBinding" Height="110" Width="300">
    <Window.Resources>
        <local:StringToEnableConverter x:Key="ste"/>
    </Window.Resources>
    <StackPanel>
        <TextBox x:Name="textBox1" BorderBrush="Black" Margin="5"/>
        <Button Height="20" Width="60" Content="button" IsEnabled="{Binding Path=Text,ElementName=textBox1,Converter={StaticResource ste}}" Margin="5"/>
    </StackPanel>
</Window>

    

3. UI元素之间的绑定(多个UI元素Binding,MutiBinding)

UI界面包含4个TextBox和一个Button,第一、二个TextBox输入内容(用户名)一致,第三、四个TextBox输入内容(密码)一致,Button可用,否则不可用

  1. 添加转换器LogonMultiBindingConverter,此类需要继承IMultiValueConverter,同样要实现Convert和ConvertBack方法,具体实现内容如下:
          public 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[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    
  1. 前台(Xaml)页面仅绘出页面,代码如下:
      <Window x:Class="WpfApp9.Window5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MutiBinding" Height="185" Width="300">
    <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>

    
  1. 后台进行MutiBinding,代码如下:
          public partial class Window5 : Window
    {
        public Window5()
        {
            InitializeComponent();
            SetMultiBinding();
        }

        private void SetMultiBinding()
        {
            // 准备基础 Binding
            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
            MultiBinding mb = new MultiBinding() { Mode = BindingMode.OneWay };
            mb.Bindings.Add(b1);    // 注意:MultiBinding 对 Add 子 Binding 的顺序是敏感的
            mb.Bindings.Add(b2);
            mb.Bindings.Add(b3);
            mb.Bindings.Add(b4);
            mb.Converter = new LogonMultiBindingConverter();

            // 将Button 与 MultiBinding对象关联
            this.button1.SetBinding(Button.IsEnabledProperty, mb);
        }
    }

    

标签:mb,object,Binding,UI,new,WPF,public
来源: https://www.cnblogs.com/nuan22/p/16416364.html