首页 > 其他分享 >wpf 数据绑定 INotifyPropertyChanged封装

wpf 数据绑定 INotifyPropertyChanged封装

时间:2024-01-31 14:35:50浏览次数:20  
标签:INotifyPropertyChanged propertyName 绑定 storage value name wpf public string

BindableBase.cs

public abstract class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    

    // 调用方法 : public string Name { get => name; set { SetProperty<string>(ref name, value); } }
    // 1. ref T storage:这是一个引用参数,表示要设置的属性的存储字段。ref关键字意味着如果在方法内部修改了storage,那么原始变量也会被修改。
    // 2. T value:这是要设置的新值。
    // 3. [CallerMemberName] string propertyName = null:这是属性的名称。
    //    [CallerMemberName]是一个特性,它会自动将调用该方法的属性的名称作为参数传入。
    //    如果在调用SetProperty<T>()时没有提供propertyName,那么编译器会自动插入调用该方法的属性的名称。
    protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (object.Equals(storage, value)) return false;

        storage = value;
        this.OnPropertyChanged(propertyName);

        return true;
    }
    
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

src\Juster.Music\MainWindow.xaml

<Window 
        x:Class="Juster.Music.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:local="clr-namespace:Juster.Music"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">

    <Window.Resources>
        <local:UserModel x:Key="MyDataSource"/>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" HorizontalAlignment="Center">
            <Label>Enter a Name:</Label>
            <TextBox>
                <Binding Source="{StaticResource MyDataSource}" Path="Name"
                   UpdateSourceTrigger="PropertyChanged" />
            </TextBox>
        </StackPanel>

        <StackPanel Grid.Row="1" HorizontalAlignment="Center">
            <Label>The name you entered:</Label>
            <TextBlock Text="{Binding Source={StaticResource MyDataSource}, Path=Name}"/>
        </StackPanel>

    </Grid>
</Window>

src\Juster.Music\MainWindow.xaml.cs


/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    
}

UserModel.cs

public class UserModel : BindableBase
{
    private string name;
    // 调用父类的SetProperty()方法 
    public string Name { get => name; set { SetProperty<string>(ref name, value); } }
    public UserModel(){ }
}

标签:INotifyPropertyChanged,propertyName,绑定,storage,value,name,wpf,public,string
From: https://www.cnblogs.com/zhuoss/p/17999186

相关文章

  • c# ComboBox控件的常用一些属性和用法、事件及数据绑定方法
    一、常用属性和用法1、Text:获取或设置与此控件关联的文本。//设置默认值this.comboBox1.Text="请选择内容";//orcomboBox1.Items.Add("请选择内容");comboBox1.SelectedIndex=0;2、SelectedIndex:获取或设置指定当前选定项的索引。(设置新索引会SelectedIndexChanged......
  • wpf 数据绑定 执行流程
    数据绑定SimpleBinding\MainWindow.xaml<Windowx:Class="SimpleBinding.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"......
  • WPF 列表内容跟值来排布位置
    首先是利用 ItemsControl来随机(或者根据绑定传的值)来分布位置主要是用Canvas来当画布,然后由值来调整位置 首先,创建实体类publicclassClassA{publicdoubleUpTop{get;set;}publicdoubleUpLeft{get;set;}}然后再你的ViewModel调用publiccl......
  • Prism:打造WPF项目的MVVM之选,简化开发流程、提高可维护性
     概述:探索WPF开发新境界,借助PrismMVVM库,实现模块化、可维护的项目。强大的命令系统、松耦合通信、内置导航,让您的开发更高效、更流畅在WPF开发中,一个优秀的MVVM库是Prism。以下是Prism的优点以及基本应用示例:优点:模块化设计: Prism支持模块化开发,使项目更易维护和扩展。......
  • 通过Demo学WPF—数据绑定(一)✨
    前言✨想学习WPF,但是看视频教程觉得太耗时间,直接看文档又觉得似懂非懂,因此想通过看Demo代码+文档的方式进行学习。准备✨微软官方其实提供了WPF的一些Demo,地址为:microsoft/WPF-Samples:RepositoryforWPFrelatedsamples(github.com)将其克隆到本地,有很多的Demo代码:新建......
  • 3.数据绑定
    什么是MVVM?看一张图。View负责数据的输入与输出;ViewModel负责业务逻辑;Model则表示程序中具体要处理的数据。所以,Model将作为属性存在于ViewModel中,而Model最终是要显示在Ul界面(View)上的,怎么办呢?将ViewModel赋值给View的DataContext(数据上下文)属性,View就可以引用ViewModel中的那......
  • 动态绑定组件时 v-model:value 的使用
    //requireimportcomponentsconstfiles=require.context("@/components/control",true,/\index.vue$/);//console.log('files:',files.keys())//files:['./cascader/index.vue','./control/cascader/index.vue',......
  • WPF应用程序窗口
    窗口可用于1显示窗口2配置窗口的大小、位置和外观3托管特定于应用程序的内容4管理窗口的生存期窗口的属性WindowStartupLocation:窗口首次显示时的位置ShowInTaskbar:窗口是否具有任务栏按钮WindowState:指示窗口是最大化、最小化或者正常尺寸显示Topmost:是否在最顶层Icon:......
  • FluentValidation在C# WPF中的应用
    1.引言在.NET开发领域,FluentValidation以其优雅、易扩展的特性成为开发者进行属性验证的首选工具。它不仅适用于Web开发,如MVC、WebAPI和ASP.NETCORE,同样也能完美集成在WPF应用程序中,提供强大的数据验证功能。本文将深入探讨如何在C#WPF项目中运用FluentValidation进行属性验证,......
  • 2.WPF中控件类之间的继承关系
    在WPF中所有的控件都是继承DispatcherObject类,可以说在wpf中DispatcherObject是所有控件类的基类,而DispatcherObject却继承Object,而它所在的程序集是在WindowsBase.dll里。看一张图,wpf控件继承关系图 1.Shape类形状控件是WPF一大系列控件。WPF所有的形状控件都继承于Shape基......