自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊 Wpf 數(shù)據(jù)綁定實(shí)例

系統(tǒng) Windows
監(jiān)聽(tīng)事件機(jī)制,界面改變有TextChanged之類(lèi)的事件,所以改變界面可以同步修改到對(duì)象。

[[381643]]

前言:

數(shù)據(jù)綁定的基本步驟:

(1)先聲明一個(gè)類(lèi)及其屬性

(2)初始化類(lèi)賦值

(3)在C#代碼中把控件DataContext=對(duì)象;

(4)在界面設(shè)計(jì)里,控件給要綁定的屬性{Binding 綁定類(lèi)的屬性}

原理:監(jiān)聽(tīng)事件機(jī)制,界面改變有TextChanged之類(lèi)的事件,所以改變界面可以同步修改到對(duì)象

想讓普通對(duì)象實(shí)現(xiàn)數(shù)據(jù)綁定,需要實(shí)現(xiàn)INotifyPropertyChanged接口才能監(jiān)聽(tīng)ProperChanged。具體代碼如下顯示:

  1. class Person:INotifyPropertyChanged 
  2.     { 
  3.         private int age; 
  4.   
  5.         public int Age 
  6.         { 
  7.             get 
  8.             { 
  9.                 return age; 
  10.             } 
  11.             set 
  12.             { 
  13.                 this.age = value; 
  14.                 if (PropertyChanged != null
  15.                 { 
  16.                    PropertyChanged(this, 
  17.                        new PropertyChangedEventArgs("Age")); 
  18.                 } 
  19.             } 
  20.         } 

BindingMode枚舉值

名稱(chēng) 說(shuō)明
OneWay 當(dāng)源屬性變化時(shí)更新目標(biāo)屬性
TwoWay 當(dāng)源屬性變化時(shí)更新目標(biāo)屬性,當(dāng)目標(biāo)屬性變化時(shí)更新源屬性
OneTime 最初根據(jù)源屬性設(shè)置目標(biāo)屬性,其后的改變會(huì)忽略。
OneWayToSource 與OneWay類(lèi)型相似,但方向相反。
Default 此類(lèi)綁定依賴(lài)于目標(biāo)屬性

UpdateSourceTrigger

名稱(chēng) 說(shuō)明
Default 默認(rèn)值,與依賴(lài)屬性有關(guān)
Explicit 必須在顯示地調(diào)用BindingExpression.UpdateSource的情況下才更新源。
LostFocus 控件失去焦點(diǎn)的時(shí)候更新源值
PropertyChanged 綁定的目標(biāo)值改變時(shí)更新。
 

實(shí)例運(yùn)行后界面如下:

MainWindow.xaml

  1. <Window x:Class="WpfApp1.MainWindow" 
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.         xmlns:local="clr-namespace:WpfApp1" 
  7.         mc:Ignorable="d" 
  8.         Title="MainWindow" Height="600" Width="800"
  9.     <StackPanel> 
  10.         <TextBlock Text="Student ID:" FontWeight="Bold" Margin="5"/> 
  11.         <TextBox Name="textBoxId" Margin="5" Text="{Binding Id,Mode=TwoWay}"/> 
  12.         <TextBlock Text="Student Name:" FontWeight="Bold" Margin="5"/> 
  13.         <TextBox Name="textBoxName" Margin="5" Text="{Binding Name,Mode=TwoWay}"/> 
  14.         <TextBlock Text="Student List:" FontWeight="Bold" Margin="5"/> 
  15.         <ListBox Name="listBox1" Height="110" Margin="5" > 
  16.             <ListBox.ItemTemplate> 
  17.                 <DataTemplate> 
  18.                     <StackPanel Orientation="Horizontal"
  19.                         <TextBlock Text="{Binding Path=Id}" Width="30"/> 
  20.                         <TextBlock Text="{Binding Path=Name}" Width="60"/> 
  21.                         <TextBlock Text="{Binding Path=Age}" Width="30"/> 
  22.                     </StackPanel> 
  23.                 </DataTemplate> 
  24.             </ListBox.ItemTemplate> 
  25.         </ListBox> 
  26.         <ListBox Name="listBox2"  Height="80" ItemsSource="{Binding Student}" DisplayMemberPath="Id" Margin="5"/> 
  27.         <Slider Name="slider1" MinHeight="25" Value="{Binding Id}"/> 
  28.         <Grid> 
  29.             <Grid.RowDefinitions> 
  30.                 <RowDefinition Height="*"></RowDefinition> 
  31.             </Grid.RowDefinitions> 
  32.             <Grid.ColumnDefinitions> 
  33.                 <ColumnDefinition Width="*"/> 
  34.                 <ColumnDefinition Width="*"/> 
  35.             </Grid.ColumnDefinitions> 
  36.             <Button Grid.Column="0" Content="Action" FontSize="40" Name="btnCtrl1" Height="80" Margin="5" Click="BtnCtrl1_Click"/> 
  37.             <Button Grid.Column="1" Content="Action" FontSize="40" Name="btnCtrl2" Height="80" Margin="5" Click="BtnCtrl2_Click"/> 
  38.         </Grid> 
  39.     </StackPanel> 
  40. </Window> 

首先解釋下C#中的Task.Delay()和Thread.Sleep()

  1. Thread.Sleep()是同步延遲,Task.Delay()是異步延遲。
  2. Thread.Sleep()會(huì)阻塞線(xiàn)程,Task.Delay()不會(huì)。
  3. Thread.Sleep()不能取消,Task.Delay()可以。
  4. Task.Delay()實(shí)質(zhì)創(chuàng)建一個(gè)運(yùn)行給定時(shí)間的任務(wù),Thread.Sleep()使當(dāng)前線(xiàn)程休眠給定時(shí)間。
  5. 反編譯Task.Delay(),基本上講它就是個(gè)包裹在任務(wù)中的定時(shí)器。
  6. Task.Delay()和Thread.Sleep()最大的區(qū)別是Task.Delay()旨在異步運(yùn)行,在同步代碼中使用Task.Delay()是沒(méi)有意義的;在異步代碼中使用Thread.Sleep()是一個(gè)非常糟糕的主意。通常使用await關(guān)鍵字調(diào)用Task.Delay()。
  7. 我的理解:Task.Delay(),async/await和CancellationTokenSource組合起來(lái)使用可以實(shí)現(xiàn)可控制的異步延遲。

MainWindow.xaml.cs

  1. using System; 
  2. using System.Collections.ObjectModel; 
  3. using System.ComponentModel; 
  4. using System.Threading.Tasks; 
  5. using System.Windows; 
  6.  
  7. namespace WpfApp1 
  8.     /// <summary> 
  9.     /// MainWindow.xaml 的交互邏輯 
  10.     /// </summary> 
  11.     public partial class MainWindow : Window 
  12.     { 
  13.         public ObservableCollection<Student> stuList; 
  14.         public MainWindow() 
  15.         {          
  16.             InitializeComponent(); 
  17.             this.DataContext = new Student() { Name="111", Id =1 }; 
  18.             Task.Run(async() =>  //開(kāi)啟異步線(xiàn)程task 
  19.             { 
  20.                 await Task.Delay(3000); //延時(shí)3秒 
  21.                 Dispatcher.Invoke((Action)delegate //線(xiàn)程中主界面顯示需要用委托,不然這次賦值,在界面不更新 
  22.                 { 
  23.                     this.DataContext = new Student() { Name = "222", Id = 2 }; 
  24.                 }); 
  25.             });           
  26.             this.DataContext = new Student() { Name = "333" , Id = 3 }; 
  27.         } 
  28.  
  29.         private void BtnCtrl1_Click(object sender, RoutedEventArgs e) 
  30.         { 
  31.             Student stu = new Student() { Id = 4, Name = "Jon", Age = 29 }; //實(shí)例化一個(gè)Student類(lèi) 并給類(lèi)成員賦值 
  32.             this.DataContext = stu;//將實(shí)例化得對(duì)象傳給DataContext 
  33.         } 
  34.         private void BtnCtrl2_Click(object sender, RoutedEventArgs e) 
  35.         { 
  36.             ObservableCollection<Student> stuList = new ObservableCollection<Student>() //具有通知屬性的list 
  37.             { 
  38.              new Student() { Id=5, Name="Tim", Age=29 }, 
  39.              new Student() { Id=6, Name="Tom", Age=28 }, 
  40.              }; 
  41.             this.listBox1.ItemsSource = stuList; 
  42.  
  43.             this.listBox2.ItemsSource = stuList; 
  44.             this.listBox2.DisplayMemberPath = "Name"
  45.             this.DataContext = stuList; 
  46.         } 
  47.     } 
  48.     public class Student : INotifyPropertyChanged  //創(chuàng)建一個(gè)繼承自INotifyPropertyChanged的類(lèi)Student 
  49.     { 
  50.  
  51.         private string name
  52.  
  53.         public string Name 
  54.         { 
  55.             get { return name; } 
  56.             set 
  57.             { 
  58.                 name = value; 
  59.                 if (this.PropertyChanged != null
  60.                 { 
  61.                     PropertyChanged(this, new PropertyChangedEventArgs("Name")); //給Name綁定屬性變更通知事件 
  62.                 } 
  63.             } 
  64.         } 
  65.  
  66.         private int id; 
  67.  
  68.         public int Id 
  69.         { 
  70.             get { return id; } 
  71.             set 
  72.             { 
  73.                 id = value; 
  74.                 if (this.PropertyChanged != null
  75.                 { 
  76.                     this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Id"));//給Id綁定屬性變更通知事件 
  77.                 } 
  78.             } 
  79.         } 
  80.  
  81.         private int age; 
  82.  
  83.         public int Age 
  84.         { 
  85.             get { return age; } 
  86.             set 
  87.             { 
  88.                 age = value; 
  89.                 if (this.PropertyChanged != null
  90.                 { 
  91.                     this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));//給Age綁定屬性變更通知事件 
  92.                 } 
  93.             } 
  94.         } 
  95.  
  96.         public int ID { get; internal set; } 
  97.  
  98.         public event PropertyChangedEventHandler PropertyChanged; 
  99.     } 

本文轉(zhuǎn)載自微信公眾號(hào)「CSharp編程大全」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系CSharp編程大全公眾號(hào)。

 

責(zé)任編輯:武曉燕 來(lái)源: CSharp編程大全
相關(guān)推薦

2023-10-07 11:04:58

WPF數(shù)據(jù)UI

2009-12-23 15:16:52

WPF數(shù)據(jù)綁定

2009-12-24 11:15:59

WPF數(shù)據(jù)綁定

2009-12-28 09:50:08

WPF數(shù)據(jù)綁定

2023-09-28 11:42:15

2009-12-23 14:19:07

WPF單向綁定

2021-06-07 08:04:39

Restorecon命令安全

2021-08-12 18:49:41

DataStreamAPI注冊(cè)

2009-12-24 16:57:53

WPF密碼

2021-09-01 14:36:14

鴻蒙HarmonyOS應(yīng)用

2021-09-01 10:37:25

鴻蒙HarmonyOS應(yīng)用

2009-09-10 22:36:51

ASP.NET Eva

2021-02-28 22:12:11

WPF標(biāo)記Handled

2012-05-29 16:22:02

SpringMVC

2010-07-28 13:11:13

Flex數(shù)據(jù)綁定

2009-12-24 14:08:25

WPF數(shù)據(jù)模板

2010-07-28 13:31:10

Flex數(shù)據(jù)綁定

2024-11-04 15:49:43

Redis?數(shù)據(jù)遷移

2019-06-11 13:22:32

Lambda大數(shù)據(jù)架構(gòu)大數(shù)據(jù)平臺(tái)

2023-09-11 07:25:52

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)