WPF学习笔记-Binging-两个控件相互绑定与简单Binding绑定(1)

实例一  两个控件相互绑定 

第一种写法,两个控件内都使用binding

<!--通过绑定实现两个控件相互绑定-->
        <TextBox Height="23" Margin="33,88,24,0" Name="textBox1" VerticalAlignment="Top"  Text="{Binding Path=Value,ElementName=slider1}"/>
        <Slider Margin="33,131,24,110" Name="slider1" Value="{Binding Path=Text,ElementName=textBox1}" />

目标对象:TextBox;

目标对象的属性:TextBox的Text属性

数据源:slider1;

Path:数据源slider1的value值

--说明

<!-- 通过控件对应值属性,通过方法 {bingding=要绑定的控件对应的属性名称,elementName=要绑定的控件名称}-->

第二种写法

         <TextBox Height="23" Margin="33,88,24,0" Name="textBox1" VerticalAlignment="Top"  Text="{Binding Path=Value,ElementName=slider1}"/>
        <Slider Margin="33,131,24,110" Name="slider1" />

去掉了slider控件的绑定,操作的时候可以发现,当TextBox值改变后,slider没有发生改变,然后按tab键,让焦点离开TextBox,此时,Slider的值发生了改变。

注意,Binding有一个属性为UpdateSourceTrigger,该属性有四个选项,PropertyChanged\LostFocus\Explicit\Edfault。

Binding还具有NotifyOnSourceUpdated和NotifyOnTargetUpdated两个属性,如果设置为True,则当源或目标被更新后Binding会激发相应的SourceUpdated事件和TragetUpdated事件。

<TextBox Name="textBox2" Text="{Binding Path=Text.[3],ElementName=textBox1,Mode=OneWay}"></TextBox>

可以取得,绑定数据的第三个字符。

实例二,用C# 代码绑定

  /// <summary>
    /// Win0601.xaml 的交互逻辑
    /// </summary>
    public partial class Win0601 : Window
    {
        public Win0601()
        {
            InitializeComponent();
            stu = new Student();

            Binding binding = new Binding();
            binding.Source = stu;
            binding.Path = new PropertyPath("Name");

            BindingOperations.SetBinding(this.textBoxName, TextBox.TextProperty, binding);
        }
        Student stu;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            stu.Name += "Name";
        }

    }

    class Student : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string m_name;
        public string Name
        {
            get { return m_name; }
            set { 
                m_name = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(thisnew PropertyChangedEventArgs("Name"));
            }
        }
    }

  实例三

class City
        {
            public string Name { getset; }
        }
        class Province
        {
            public string Name { getset; }
            public List<City> CityList { getset; }
        }
        class Country
        {
            public string Name { getset; }
            public List<Province> ProvinceList { getset; }
        }

//binging

this.textBox1.Setbinding(TextBox.TextProperty,new binding("/Name")){source=countryList};

this.textBox2.Setbinding(TextBox.TextProperty,new binding("/ProvinceList.Name")){source=countryList};

this.textBox3.Setbinding(TextBox.TextProperty,new binding("/Provinces/CityList.Name")){source=countryList};
 

 

 为什么要定义一个类型:为了方便尽量不操作控件。MVVM、MVC。
1、定义类,定义属性。
2、new一个类的实例,给要绑定的控件设定DataContext
txtName.DataContext =p1;
3、XAML中要进行数据绑定的属性
Text="{Binding Name}",几乎所有属性都能进行这样的数据绑定。
Text="{Binding Name}",把控件的Text属性绑定到Datacontext指向的p1对象的Name属性上。

本文部分来自温馨小筑的博客,原文地址:http://hi.baidu.com/%D0%A1%C5%AE%CE%D7%D2%C0%D2%C0/blog/item/8fe04011bb7b020cb8127b2a.html

posted @ 2013-05-27 13:31  秋水惜朝  阅读(1915)  评论(1编辑  收藏  举报