WPF 中雙向繫結通知機制

2021-10-03 10:16:19 字數 3854 閱讀 1183

inotifypropertychanged實現

inotifypropertychanged會向客戶端發出某一屬性值已更改的通知。當元素屬性值改變時,會通知後台model

前台**不變,我們讓後台students model實現inotifypropertychanged介面。

通過datacontext實現資料資料項的變更通知。新增乙個viewmodel類,實現inotifypropertychanged介面

前台xmal

x:class

= xmlns

=""xmlns:x

=""xmlns:d

=""xmlns:mc

=""mc:ignorable

="d"

title

="mainwindow"

height

="450"

width

="800"

>

>

height

="295"

width

="427"

horizontalalignment

="left"

verticalalignment

="top"

margin

="10,10,0,0"

name

="stackpane1"

>

height

="23"

name

="txtblock1"

text

="學員編號"

>

textblock

>

height

="23"

name

="txtstudentid"

width

="301"

horizontalalignment

="left"

>

textbox

>

height

="23"

name

="txtblock2"

text

="學員列表"

>

textblock

>

height

="156"

name

="lbstudent"

width

="305"

horizontalalignment

="left"

itemssource=""

>

>

>

name

="stackpanel2"

orientation

="horizontal"

>

text=""

margin

="5"

background

="beige"

>

textblock

>

text=""

margin

="5"

>

textblock

>

text=""

margin

="5"

>

textblock

>

stackpanel

>

datatemplate

>

listbox.itemtemplate

>

listbox

>

content

="button"

height

="23"

name

="button1"

width

="75"

horizontalalignment

="left"

click

="button1_click"

>

button

>

stackpanel

>

grid

>

window

>

後台cs
using system.collections.objectmodel;

using system.componentmodel;

using system.windows;

using system.windows.controls;

using system.windows.data;

///

/// - 欄位或屬性實現inotifypropertychanged介面,實現了該介面,只要欄位或屬性的發生了改變,就會提供通知機制

/// - wpf 提供 observablecollection類,它是實現 inotifycollectionchanged 介面的資料集合的內建實現,以便集合中的插入或刪除操作可以自動更新 ui

///

,new

students()

,new

students()

,new

students()

};this

.lbstudent.datacontext = viewmodel;

this

.txtstudentid.

setbinding

(textbox.textproperty,

newbinding

("selecteditem.id"))

;}private

void

button1_click

(object sender,

routedeventargs e)

;

viewmodel.studentlist =

newobservablecollection

<

students

>()

,new

students()

,new

students()

,new

students()

};viewmodel.studentlist[2]

.name =

"這是乙個屬性改變";}

public

class

students

:inotifypropertychanged

string _name;

public

string name

set}

public

int age

protected

virtual

void

onpropertychanged

(string propertyname)

}public

event

propertychangedeventhandler propertychanged;

}public

class

viewmodel

:inotifypropertychanged

set}

}public

event

propertychangedeventhandler propertychanged;

private

void

onpropertychanged

(string propertyname)}}

}}

WPF資料雙向繫結

設定雙向繫結,首先控制項要繫結的物件要先繼承乙個介面 inotifypropertychanged 然後對應被繫結的屬性增加 如下 意思就是當age這個屬性變化時,要通知監聽它變化的人。即 propertychanged this,new propertychangedeventargs age p...

wpf資料繫結更新通知

類似於這樣子的 大致的意思是 乙個 變數,通過改變變數的值,繫結這個變數的這個圓顏色也在變化 就是一種心跳效果 在網上資料觸發的感覺不多,廢了不少時間,這裡做個總結 1 通知 class notifybase inotifypropertychanged class notifymodels not...

WPF雙向資料繫結總結

參考官方 例項程式 1 指定繫結源 wpf雙向資料同步 目標屬性 ui 和源屬性 cs 資料同步。實現雙向資料同步資料來源需要實現依賴屬性inotifypropertychanged介面,因為依賴屬性有垂直的內嵌變更通知機制。inotifypropertychanged是用於實現介面通知。depen...