【代码积累】Binding的常用写法

1、绑定对象

<span style="font-family:Microsoft YaHei;font-size:14px;">public basepage1()
        {
            InitializeComponent();

            //准备数据源
            stu = new Student();

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

            //使用Binding连接数据源与Binding目标
            BindingOperations.SetBinding(this.textBoxName, TextBox.TextProperty, binding);
        }</span>
      如上述代码所述,实例化一个Student类,将Student对象的Name属性与TextBox控件进行绑定,基本的三步式操作,简化版本如下:

<span style="font-family:Microsoft YaHei;font-size:14px;">public basepage1()
        {
            InitializeComponent();
            //一步到位,之前可以加上对数据源的为空验证
            if (this.textBoxName.Text == "")
                MessageBox.Show("数据源为空哦~");
            //将控件、数据源直接绑定
            this.textBoxName.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu = new Student() });

        }</span>

2、绑定DataView

<span style="font-family:Microsoft YaHei;font-size:14px;">public partial class DataView : Page
{
     List<string> stringList = new List<string>() { "Tim", "Tom", "Blog" };
     public DataView()
     {
         InitializeComponent();

         this.textBox1.SetBinding(TextBox.TextProperty, new Binding("/") { Source = stringList });
         this.textBox2.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = stringList, Mode = BindingMode.OneWay });
         this.textBox3.SetBinding(TextBox.TextProperty, new Binding("/[2]") { Source = stringList, Mode = BindingMode.OneWay });
     }
}</span>
       如上述代码所述,通过textBox和一个DataView中的string类型集合进行绑定,方法类似于1中第二种方法类型,

       1、new Binding("/")  

       2、new Binding("/Length")

       3、new Binding("/[2]")

      分别绑定整个集合、绑定其长度、绑定其中第三个对象,就是这么简单哦。


3、绑定方法返回值

<span style="font-family:Microsoft YaHei;font-size:14px;">private void button_Click(object sender, RoutedEventArgs e)
{
     //ObjectDataProvider用来使得方法返回值,作为Binding数据源
     ObjectDataProvider odp = new ObjectDataProvider();
     odp.ObjectInstance = new Calculator();
     odp.MethodName = "Add";
     odp.MethodParameters.Add("100");
     odp.MethodParameters.Add("200");
     MessageBox.Show(odp.Data.ToString());
}</span>
      如上述代码,是在前台xaml页面的.cs文件中写的,通过引入ObjectDataProvider对象,实现将方法的返回值送到前台进行显示的功能,注意理解"MethodParameters.Add"的应用。


4、绑定到ItemSource上

<span style="font-family:Microsoft YaHei;font-size:14px;">public ItemsSource()
        {
            InitializeComponent();

            //准备数据源
            List<Student> stuList = new List<Student>()
            {
                new Student() { Id=0,Name="Tim",Age=29},
                new Student() { Id=1,Name="Tom",Age=21},
                new Student() { Id=2,Name="Kyle",Age=49},
                new Student() { Id=3,Name="Tony",Age=22},
                new Student() { Id=4,Name="Vina",Age=25},
                new Student() { Id=5,Name="Mike",Age=23}
            };

            //为ListBox设置Binding
            this.listBoxStudents.ItemsSource = stuList;
            this.listBoxStudents.DisplayMemberPath = "Name";

            //为TextBox设置Binding
            Binding binding = new Binding("SelectedItem.Id") { Source = this.listBoxStudents };
            this.textBoxId.SetBinding(TextBox.TextProperty, binding);
        }</span>
      如上所述,仍然是List集合充当数据源,前台分别绑定到ListBox下拉框以及TextBox文本框当中,对于ListBox,DisplayMemberPath默认属性选择一个就ok了,而对于TextBox,选择其中一个字段进行绑定,则是通过实例化Binding时的("SelectedItem.id")进行。

       

      综上所述,针对最近使用MVVM中binding用到的一些用法进行总结,不然时间久了又就忘了。






评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值