惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
aimingoo的专栏
aimingoo的专栏
腾讯CDC
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园_首页
B
Blog RSS Feed
D
Docker
M
MIT News - Artificial intelligence
爱范儿
爱范儿
I
InfoQ

博客园 - 飘扬De黑夜

SqlServer 批量修改某个字段类型 U8数据库表汇总 FastReport 使用JSON作为数据源,手工赋值和导出PDF elementui关于表单校验validator Vue.js 2 使用 extends 扩展 element-ui 组件 FastReport-Barcode/QRCode 控件总是存在白色空白区域 双网卡同时访问内外网 vs项目启动后总是提示ID为XXX的进程当前未运行 Vue2全局管理格式化器和Filter uniapp 以npm模式安装插件的项目,如何让自定义组件支持easycom方式自动导入 Http请求中 Content-Type 常用格式说明 WPF入门之设置样式 ElementUI-如何给 el-dropdown 的 command 事件传递多个参数 TortoiseGit使用 管理系统中台示例参照 淘宝源安装npm visual studio 2022 自定义组件和自定义控件和用户控件的区别 SqlServer GoupBy 分组后对于非分组的某个字符串进行拼接 SQL Server 实现类似CountIF的函数
WPF入门基础之双向数据绑定
飘扬De黑夜 · 2025-03-19 · via 博客园 - 飘扬De黑夜

有一个比较重要的点就是,继承INotifyPropertyChanged,并实现OnPropertyChanged。

viewModel实现代码如下:

public class MainWindowViewModel:INotifyPropertyChanged
{
    private bool _isBinding = false;
    public bool IsBinding
    {
        get
        {
            return _isBinding;
        }
        set
        {
            this._isBinding = value;
            OnPropertyChanged("IsBinding");
            // 通知UI界面更新绑定BindingText值的控件进行数据更新
            OnPropertyChanged("BindingText");
        }
    }

    public string BindingText
    {
        get
        {
            return this.IsBinding ? "绑定" : "空闲";
        }
    }

    private string _bindingName="MyBindingName";

    public string BindName 
    { 
        get
        {
            return _bindingName;
        }
        set
        {
            this._bindingName = value;
            OnPropertyChanged("BindName");
        } 
    }

    public event PropertyChangedEventHandler? PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}

窗体后台代码如下:

public partial class MainWindow : Window
{
    public MainWindowViewModel ViewModel {  get; set; } = new MainWindowViewModel();

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = ViewModel;


    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.ViewModel.BindName = "更改";
    }
}

xaml前端代码如下:

 <StackPanel>
     <TextBlock Text="{Binding BindName}" Height="30" Width="220" Background="Aquamarine" />
     <ToggleButton x:Name="MySwitch" IsChecked="{Binding IsBinding}" Content="OFF" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="50" Style="{StaticResource SwitchStyle}"/>
     <Button Content="Button" Height="55" Click="Button_Click"/>
 </StackPanel>

单击按钮发现值随之更改!!!