C#?wpf定義ViewModelBase進(jìn)行簡(jiǎn)化屬性綁定
概述
綁定機(jī)制是wpf的核心,也是界面獨(dú)立的根本,尤其是使用了mvvm模式,沒(méi)有業(yè)務(wù)邏輯的干擾,使得界面得以專注于絢麗效果的實(shí)現(xiàn)。在xaml中編寫(xiě)綁定語(yǔ)句后,在業(yè)務(wù)邏輯層需要定義相應(yīng)的屬性與其綁定,需要繼承INotifyPropertyChanged,并通過(guò)PropertyChanged通知屬性改變。但是每個(gè)地方都去繼承實(shí)現(xiàn)INotifyPropertyChanged還是有點(diǎn)麻煩。
具體方法
1、繼承INotifyPropertyChanged
繼承INotifyPropertyChanged后需要定義一個(gè)事件。
public event PropertyChangedEventHandler PropertyChanged;
2、定義通知方法
定義一個(gè)動(dòng)作屬性改變的方法。這一步驟將2個(gè)參數(shù),簡(jiǎn)化成了一個(gè)字符串參數(shù)。
protected void RaisePropertyChangedEvent( string propertyName ) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
3、自動(dòng)獲取屬性名
調(diào)用的時(shí)候要寫(xiě)屬性名還是有點(diǎn)麻煩,進(jìn)一步簡(jiǎn)化,將屬性名參數(shù)省去。
在.net 4.5以下需要通過(guò)棧幀和反射獲取屬性名,在.net4.5或core1.0以上使用System.Runtime.CompilerServices.CallerMemberName屬性即可。
string GetCallerMemberName() { StackTrace trace = new StackTrace(); StackFrame frame = trace.GetFrame(2);//1代表上級(jí),2代表上上級(jí),以此類推 var propertyName = frame.GetMethod().Name; if (propertyName.StartsWith("get_") || propertyName.StartsWith("set_") || propertyName.StartsWith("put_")) { propertyName = propertyName.Substring("get_".Length); } return propertyName; }
通知方法作相應(yīng)的修改如下。
protected void RaisePropertyChangedEvent(/*此屬性在.net 4.5以下不支持將其注釋即可*/[System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") { if (propertyName == "") { propertyName = GetCallerMemberName(); } if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
注:在.net4.5以下只能調(diào)用GetCallerMemberName,使用者覺(jué)得影響性能則直接使用參數(shù)賦值即可。
完整代碼
public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChangedEvent([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") { if (propertyName == "") { propertyName = GetCallerMemberName(); } if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } string GetCallerMemberName() { StackTrace trace = new StackTrace(); StackFrame frame = trace.GetFrame(2);//1代表上級(jí),2代表上上級(jí),以此類推 var propertyName = frame.GetMethod().Name; if (propertyName.StartsWith("get_") || propertyName.StartsWith("set_") || propertyName.StartsWith("put_")) { propertyName = propertyName.Substring("get_".Length); } return propertyName; } }
使用示例
TimeTick .cs
public class TimeTick : ViewModelBase { public double Seconds { set { _seconds = value; RaisePropertyChangedEvent(); } get { return _seconds; } } double _seconds; public TimeTick() { DispatcherTimer time = new DispatcherTimer(); time.Tick += Time_Tick; time.Interval = TimeSpan.FromMilliseconds(1000); time.Start(); } private void Time_Tick(object sender, EventArgs e) { //修改屬性的值 Seconds++; } }
MainWindow.xaml.cs
public MainWindow() { InitializeComponent(); this.DataContext = new TimeTick(); }
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="360" Width="640"> <Grid > <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Seconds}" FontSize="128" Foreground="#999999" Loaded="TextBlock_Loaded" ></TextBlock> </Grid> </Window>
顯示效果
到此這篇關(guān)于C# wpf定義ViewModelBase進(jìn)行簡(jiǎn)化屬性綁定的文章就介紹到這了,更多相關(guān)wpf ViewModelBase簡(jiǎn)化屬性綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換的示例詳解
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換,例如轉(zhuǎn)換成圖標(biāo)圖像ICO,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01C#根據(jù)權(quán)重抽取隨機(jī)數(shù)
最近在開(kāi)發(fā)過(guò)程中遇到一個(gè)需要做帶權(quán)隨機(jī)的處理,本文主要介紹了C#根據(jù)權(quán)重抽取隨機(jī)數(shù),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02WinForm使用正則表達(dá)式提取內(nèi)容的方法示例
這篇文章主要介紹了WinForm使用正則表達(dá)式提取內(nèi)容的方法,結(jié)合實(shí)例形式分析了WinForm基于正則匹配獲取指定內(nèi)容的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05C#調(diào)用SQL語(yǔ)句時(shí)乘號(hào)的用法
這篇文章主要介紹了C#調(diào)用SQL語(yǔ)句時(shí)乘號(hào)的用法,可避免因符號(hào)引起的程序錯(cuò)誤,是C#程序設(shè)計(jì)人員有必要掌握的,需要的朋友可以參考下2014-08-08詳解C#如何實(shí)現(xiàn)一個(gè)安全的事件訂閱器
事件訂閱器是一個(gè)對(duì)象,它訂閱(或監(jiān)聽(tīng))某個(gè)事件,并在事件發(fā)生時(shí)執(zhí)行相應(yīng)的操作,本文主要介紹了C#實(shí)現(xiàn)一個(gè)安全的事件訂閱器的相關(guān)知識(shí),感興趣的可以了解下2024-01-01C#通過(guò)html調(diào)用WinForm的方法
這篇文章主要介紹了C#通過(guò)html調(diào)用WinForm的方法,涉及html頁(yè)面中使用JavaScript訪問(wèn)C#的相關(guān)技巧,需要的朋友可以參考下2016-04-04DevExpress實(shí)現(xiàn)TreeList父子節(jié)點(diǎn)CheckState狀態(tài)同步的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList父子節(jié)點(diǎn)CheckState狀態(tài)同步的方法,需要的朋友可以參考下2014-08-08