WPF 绑定表达式和绑定数据源(一)

关于绑定:[Binding]

绑定:描述的是一种关系,同构某种关系将多个事物联系在一起。
  • ​ 页面对象的属性(必须是依赖属性):目标 Target

  • ​ 需要显示在界面上做交互关联的数据对象 :源 Source

    
        
            
        
    
      //备注:
      //Text:目标
      //Source=data,Path=value   源:  也是绑定表达式
                
    
1、绑定表达式
Text="{Binding Source=data,Path=value}" :将页面对象的某个属性与数据源建立联系。

​ 通过绑定可以将界面与数据逻辑进行隔离。

2、绑定数据源
1、指定方式:

​ Source、ElementName、DataContext、RelativeSource、Path、XPath

​ 2、数据源类型

​ 1)依赖对象作为数据源


    
        
          
        
            
            


            
            
            
            
            
         
    


​ 2) 普通数据类型或集合类型作为数据源


        小明
        
            小刚
        
    
    
     
            
            
            

​ 3) 单个对象作为数据源,INotifyPropertyChanged

新建类:DataClass 继承 INotifyPropertyChanged

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp2
{
    public class DataClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;
        private string  _Value;
        public string Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
            }

        }

    }
}

xaml代码:

 
        
     



      

xaml.cs 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp2
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Task.Run(() => {
                Task.Delay(2000).GetAwaiter().GetResult();
                this.Dispatcher.Invoke(() => {
                    (this.Resources["dataclass"] as DataClass).Value = "";
                });
            });
        }
    }
}

​ 4)XmlDataProvider作为数据源

新建一个XML文件:【simple.xml】


<breakfast_menu>
	<food attr="a1" prop="p1">
		<name>Belgian Wafflesname>
		<price>$5.95price>
		<description>two of our famous Belgian Waffles with plenty of real maple syrupdescription>
		<calories>650calories>
	food>
	<food attr="a2">
		<name>Strawberry Belgian Wafflesname>
		<price>$7.95price>
		<description>light Belgian waffles covered with strawberries and whipped creamdescription>
		<calories>900calories>
	food>
	<food attr="a3">
		<name>Berry-Berry Belgian Wafflesname>
		<price>$8.95price>
		<description>light Belgian waffles covered with an assortment of fresh berries and whipped creamdescription>
		<calories>900calories>
	food>
	<food attr="a4">
		<name>French Toastname>
		<price>$4.50price>
		<description>thick slices made from our homemade sourdough breaddescription>
		<calories>600calories>
	food>
	<food attr="a5">
		<name>Homestyle Breakfastname>
		<price>$6.95price>
		<description>two eggs, bacon or sausage, toast, and our ever-popular hash brownsdescription>
		<calories>950calories>
	food>
breakfast_menu>

xaml 代码如下:

//d第一种读取方式   读取对象属性                                                    //第二种读取方式   读取对象属性                                       //备注:xml文件多节点下标是从1开始的。  

​ 5)ObjectDataProvider作为数据源

​ 新建类:【MethodClass】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp2
{
    public  class MothedClass
    {
        public double GetValue(string value)
        {
            return double.Parse(value) * 0.5;
        }
    }
}


xmal 代码如下:


    
        
            
                
                    200
                
            
        
    
    
        
            
        
    
 

​ 6) 静态对象属性作为数据源

​ 新建静态类:[StaticClass]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp2
{
    public  class StaticClass
    {  
        public static int MyProperty { get; set; } = 555;
        //通知  第一种方式  [Value]Changed  必须与属性名称一一对应【不推荐】
        public static  event  EventHandler ValueChanged;
        //通知  第二种方式
        public static event EventHandler StaricPropertyChanged;

        private static int value { get; set; }=66;
        public static int Value
        {
            get { return value; }
            set { Value = value;
                //通知  第一种方式 【不推荐】
                // ValueChanged?.Invoke(null,new  PropertyChangedEventArgs("Value"));
                //通知  第二种方式
                StaricPropertyChanged?.Invoke(null, new PropertyChangedEventArgs("Value"));
            }
        }  
    }
}

xaml:代码如下

 
    
         
             
            
            
            
            
          
    


你可能感兴趣的:(上位机,wpf,microsoft,c#)