2024年终活动

DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

界面控件DevExpress WPF入门级教程:MVVM 框架 - BindableBase

来源:   发布时间:2022-04-07   浏览:1159次

DevExpress WPF v21.2正式版下载

BindableBase类实现 INotifyPropertyChanged 接口,并提供 API 以最少的编码声明可绑定属性。

可绑定属性

要声明可绑定属性,请执行以下操作:

  • 从 BindableBase 类继承您的 ViewModel;
  • 在属性 getter 和 setter 中使用 GetValue 和 SetValue 方法。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
public string FirstName {
get { return GetValue<string>(nameof(FirstName)); }
set { SetValue(value, nameof(FirstName)); }
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Public Property FirstName() As String
Get
Return GetValue(Of String)(NameOf(FirstName))
End Get
Set(ByVal value As String)
SetValue(value, NameOf(FirstName))
End Set
End Property
End Class

BindableBase 类公开使用 CallerMemberNameAttribute 的 GetValue 和 SetValue 重载,您不必将属性名称传递给这些方法的参数,这简化了可绑定属性声明:

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
public string FirstName {
get { return GetValue<string>(); }
set { SetValue(value); }
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Public Property FirstName() As String
Get
Return GetValue(Of String)()
End Get
Set(ByVal value As String)
SetValue(value)
End Set
End Property
End Class

注意CallerMemberNameAttribute 在 .NET Framework 4.5 及更高版本中可用。

属性值存储在内部字典中:

  • GetValue 方法使用此字典获取属性值;
  • SetValue 方法使用属性名称作为键将属性值存储在字典中。

SetValue 方法返回指示属性是否已更改的 True 或 False 值。 如果为属性设置相同的值,SetValue 方法将返回 False,并且不会发送更改通知。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
public string FirstName {
get { return GetValue<string>(); }
set {
if (SetValue(value))
NotifyFullNameChanged();
else MessageBox.Show("Could not change value!");
}
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Public Property FirstName() As String
Get
Return GetValue(Of String)()
End Get
Set(ByVal value As String)
If Not SetValue(value) Then
NotifyFullNameChanged()
Else MessageBox.Show("Could not change value!")
End If
End Set
End Property
End Class
属性值更改时运行自定义代码

SetValue 方法具有将回调方法作为参数的重载,在更改属性值后调用此回调。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
public string FirstName {
get { return GetValue<string>(); }
set { SetValue(value, changedCallback: OnFirstNameChanged); }
}
void OnFirstNameChanged() {
//...
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Public Property FirstName() As String
Get
Return GetValue(Of String)()
End Get
Set(ByVal value As String)
SetValue(value, changedCallback:= AddressOf OnFirstNameChanged)
End Set
End Property
Sub OnFirstNameChanged()
'...
End Sub
End Class
手动引发 INotifyPropertyChanged.PropertyChanged

如果您需要为特定属性引发 INotifyPropertyChanged.PropertyChanged 事件,请使用 RaisePropertyChanged/RaisePropertiesChanged 方法。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
public string FirstName {
get { return GetValue<string>(); }
set { SetValue(value, changedCallback: NotifyFullNameChanged); }
}

public string LastName {
get { return GetValue<string>(); }
set { SetValue(value, changedCallback: NotifyFullNameChanged); }
}

public string FullName { get { return FirstName + " " + LastName; } }

void NotifyFullNameChanged() {
RaisePropertyChanged(nameof(FullName));
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Public Property FirstName() As String
Get
Return GetValue(Of String)()
End Get
Set(ByVal value As String)
SetValue(value, changedCallback:= AddressOf NotifyFullNameChanged)
End Set
End Property

Public Property LastName() As String
Get
Return GetValue(Of String)()
End Get
Set(ByVal value As String)
SetValue(value, changedCallback:= AddressOf NotifyFullNameChanged)
End Set
End Property

Public ReadOnly Property FullName() As String
Get
Return FirstName & " " & LastName
End Get
End Property

Private Sub NotifyFullNameChanged()
RaisePropertyChanged(Function() FullName)
End Sub
End Class
使用带有支持字段的属性

当一个属性被频繁访问或更新时,应用程序的性能可能会降低,因为属性值存储在字典中。 要提高应用程序的性能,请将属性值存储在支持字段中。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
string _FirstName;
public string FirstName {
get { return _FirstName; }
set { SetValue(ref _FirstName, value, changedCallback: NotifyFullNameChanged); }
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Private _FirstName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
SetValue(_FirstName, value, changedCallback:= AddressOf NotifyFullNameChanged)
End Set
End Property
End Class
.NET Framework 4.0 中的 BindableBase

可绑定属性

使用 GetProperty 和 SetProperty 方法来实现可绑定属性。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
public string FirstName {
get { return GetProperty(() => FirstName); }
set { SetProperty(() => FirstName, value); }
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Public Property FirstName As String
Get
Return GetProperty(Function() FirstName)
End Get
Set(value As String)
SetProperty(Function() FirstName, value)
End Set
End Property
End Class

GetProperty 和 SetProperty 方法的第一个参数是一个 lambda 表达式,它返回用于标识目标属性名称的属性。 属性名称是使用静态 BindableBase.GetPropertyName<T> 方法在内部获取的。

属性值更改时运行自定义代码

SetProperty 方法返回指示属性是否已更改的 True 或 False 值。如果将该属性设置为相同的值,SetProperty 方法将返回 False,并且不会发送更改通知。 SetProperty 方法具有将回调方法作为参数的重载,此回调在字段更改后调用。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
public string FirstName {
get { return GetProperty(() => FirstName); }
set { SetProperty(() => FirstName, value, OnFirstNameChanged); }
}
void OnFirstNameChanged() {
//...
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Public Property FirstName As String
Get
Return GetProperty(Function() FirstName)
End Get
Set(value As String)
SetProperty(Function() FirstName, value, AddressOf OnFirstNameChanged)
End Set
End Property
Sub OnFirstNameChanged()
'...
End Sub
End Class

手动引发 INotifyPropertyChanged.PropertyChanged

如果您需要为特定属性引发 INotifyPropertyChanged.PropertyChanged 事件,请使用 RaisePropertyChanged/RaisePropertiesChanged 方法。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
public string FullName {
get { return string.Format("{0} {1}", FirstName, LastName); }
}
public string FirstName {
get { return GetProperty(() => FirstName); }
set { SetProperty(() => FirstName, value, OnFirstNameChanged); }
}
public string LastName {
get { return GetProperty(() => LastName); }
set {
if(SetProperty(() => LastName, value))
RaisePropertyChanged(() => FullName);
}
}
void OnFirstNameChanged() {
RaisePropertyChanged(() => FullName);
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Public ReadOnly Property FullName As String
Get
Return String.Format("{0} {1}", FirstName, LastName)
End Get
End Property
Public Property FirstName As String
Get
Return GetProperty(Function() FirstName)
End Get
Set(value As String)
SetProperty(Function() FirstName, value, AddressOf OnFirstNameChanged)
End Set
End Property
Public Property LastName As String
Get
Return GetProperty(Function() LastName)
End Get
Set(value As String)
If SetProperty(Function() LastName, value) Then
RaisePropertyChanged(Function() FullName)
End If
End Set
End Property
Sub OnFirstNameChanged()
RaisePropertyChanged(Function() FullName)
End Sub
End Class

使用带有支持字段的属性

当属性被频繁更新时(由于从 lambda 表达式计算属性名称并将属性值存储在字典中),应用程序性能可能会降低。 要适应这些情况,请使用存储变量来存储属性,并使用 BindableBase.GetPropertyName<T> 方法从静态构造函数计算一次属性名称。

C#

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
static string Property1Name;
static ViewModel() {
Property1Name = BindableBase.GetPropertyName(() => new ViewModel().Property1);
}

string property1;
public string Property1 {
get { return property1; }
set { SetProperty(ref property1, value, Property1Name); }
}
}

VB.NET

Imports DevExpress.Mvvm

Public Class ViewModel
Inherits BindableBase
Shared Property1Name As String
Shared Sub New()
Property1Name = BindableBase.GetPropertyName(Function() New ViewModel().Property1)
End Sub

Dim _Property1 As String
Public Property Property1 As String
Get
Return _Property1
End Get
Set(value As String)
SetProperty(_Property1, value, Property1Name)
End Set
End Property
End Class

DevExpress WPF | 下载试用

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。


DevExpress技术交流群6:600715373      欢迎一起进群讨论

更多DevExpress线上公开课、中文教程资讯请上中文网获取

DevExpress正版化活动火热进行中
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/2986.html

相关产品: DevExpress Universal Subscription,

扫码咨询
电话咨询
023-68661681
返回
顶部