zoukankan      html  css  js  c++  java
  • Binding

    1)数据源 

    添加一个Student类:

    (Binding是一种自动机制,当值变化后属性要有能力通知Binding,让Binding把变化传递给UI元素。方法是在属性set语句中激发一个PropertyChanged事件。这个事件不用我们声明,我们要做到的是让作为数据源的类实现System.ComponentModel名称空间中的INotifyPropertyChanged接口。当为Binding设置了数据源后,Binding就会自动监听来自这个接口的PropertyChanged事件。)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    
    namespace TestBinding
    {
        class Student:INotifyPropertyChanged//实现这个接口,让Binding自动监听
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private string name;
    
            public string Name
            {
                get { return name; }
                set {
                    name = value;
                    //激发事件
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
                    }
                }
            }
        }
    }

    或者:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    
    namespace TestBinding
    {
        class Student:INotifyPropertyChanged//实现这个接口,让Binding自动监听
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private string name;
    
            public string Name
            {
                get { return name; }
                set {
                    name = value;
                    //激发事件
                    PropertyChangedEventHandler handler = PropertyChanged;
                    if (this.PropertyChanged != null)
                    {
                        //this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
                        handler(this, new PropertyChangedEventArgs("Name"));
                    }
                }
            }
        }
    }

    2)编写前台代码:

    XAML:

    <Window x:Class="TestBinding.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="110" Width="300">
        <Grid>
            <StackPanel>
                <TextBox x:Name="textBoxName" BorderBrush="Black" Margin="5"></TextBox>
                <Button Content="Add Age" Margin="5" Click="Button_Click"></Button>
            </StackPanel>
        </Grid>
    </Window>

    3)使用Binding把数据源和UI元素连接起来

    C#:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace TestBinding
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            Student stu;
            public MainWindow()
            {
                InitializeComponent();
    
                /*
                //准备数据源
                stu = new Student();
    
                //准备Binding
                Binding binding = new Binding();
                binding.Source = stu;
                binding.Path = new PropertyPath("Name");//为Binding指定访问路径
    
                //使用Binding连接数据源与Binding目标
                BindingOperations.SetBinding(textBoxName, TextBox.TextProperty, binding);
                */
    
                //以上三合一的做法:
                this.textBoxName.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu = new Student() });
                //this.textBoxName.SetBinding(TextBox.TextProperty, new Binding() { Source = stu = new Student(),Path=new PropertyPath("Name") });
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                stu.Name += "Name";
            }
        }
    }

    截图:

  • 相关阅读:
    VScode 修改中文字体
    missing KW_END at ')' near '<EOF>'
    SQL inner join, join, left join, right join, full outer join
    SQL字符替换函数translater, replace
    SQL COOKBOOK SQL经典实例代码 笔记第一章代码
    sqlcook sql经典实例 emp dept 创建语句
    dateutil 2.5.0 is the minimum required version python
    安装postgresql后找不到服务 postgresql service
    Postgres psql: 致命错误: 角色 "postgres" 不存在
    【西北师大-2108Java】第十六次作业成绩汇总
  • 原文地址:https://www.cnblogs.com/KeenLeung/p/3523438.html
Copyright © 2011-2022 走看看