栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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】



	
		Belgian Waffles
		$5.95
		two of our famous Belgian Waffles with plenty of real maple syrup
		650
	
	
		Strawberry Belgian Waffles
		$7.95
		light Belgian waffles covered with strawberries and whipped cream
		900
	
	
		Berry-Berry Belgian Waffles
		$8.95
		light Belgian waffles covered with an assortment of fresh berries and whipped cream
		900
	
	
		French Toast
		$4.50
		thick slices made from our homemade sourdough bread
		600
	
	
		Homestyle Breakfast
		$6.95
		two eggs, bacon or sausage, toast, and our ever-popular hash browns
		950
	


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:代码如下

 
    
         
             
            
            
            
            
          
    


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985317.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号