c# - 在类中实现 INotifyPropertyChanged
问题描述
我在一个类中创建了 INotifyPropertyChanged
public class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (Equals(storage, value))
{
return;
}
storage = value;
RaisePropertyChanged(propertyName);
}
protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
现在当我尝试在用户控件中使用它时
public partial class myUserControl : UserControl, BindableBase
我遇到以下错误
myUserControl 不能有多个基类
解决方案
INotifyPropertyChanged
适用于视图模型类,而不是视图(或用户控件)本身。因此,视图中通常不需要它们。如果要将字段添加到用户控件,则应改为使用依赖属性。
请参阅UserControl上的示例:
/// <summary>
/// Identifies the Value dependency property.
/// </summary>
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value", typeof(decimal), typeof(NumericUpDown),
new FrameworkPropertyMetadata(MinValue, new PropertyChangedCallback(OnValueChanged),
new CoerceValueCallback(CoerceValue)));
/// <summary>
/// Gets or sets the value assigned to the control.
/// </summary>
public decimal Value
{
get { return (decimal)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
推荐阅读
- algorithm - 运送人群的最少汽车数量
- google-drive-api - 在 Google Drive 和 Google Cloud Shell Editor 之间链接和同步文件夹
- axios - 在发送请求之前检查 api 端点是否退出 - axios
- javascript - 替换文本但如果包含特定字符则不替换?
- flutter - 如何使用 TDLib Telegram 库保持会话/保存用户身份验证以进行颤动?
- javascript - 在 Typescript/Javascript 中使用 .filter 方法时如何对临时变量进行类型检查
- powerbi - 分割在 pbix 的表/矩阵可视化中不起作用?
- php - 设置函数中的 if 语句
- agile - 如何在用户故事映射会话中将活动映射到任务
- javascript - 如何在柏树中更新别名