WPF中的数据绑定1——使用依赖属性

MVVM学习中,数据绑定的三种方法:

1、依赖属性

2、属性名+Changed事件

3、INotifyPropertyChanged接口

这里先介绍第一种方法,使用依赖属性进行数据绑定。

Model层:创建一个InfoModel类;

View Model层:创建一个MainViewModel类;

View层:使用MainWindow窗体;

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.ComponentModel;

namespace MVVMStudy.Model
{
    public  class InfoModel:DependencyObject
    {
        public string DPName
        {
            get { return (string)GetValue(DPNameProperty); }
            set { SetValue(DPNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DPName.  This enables animation, styling, binding, etc...
        public static readonly System.Windows.DependencyProperty DPNameProperty =
            DependencyProperty.Register("DPName", typeof(string), typeof(InfoModel), new PropertyMetadata(default(string)));

    }
}
InfoModel
复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVVMStudy.Model;

namespace MVVMStudy.ViewModel
{
    public  class MainViewModel
    {
        
        public InfoModel InfoModel { get; set; }

        public MainViewModel()
        {
            InfoModel = new InfoModel { DPName = "数据绑定" };
        }
    }
}
MainViewModel
复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MVVMStudy.ViewModel;

namespace MVVMStudy
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        MainViewModel model = new MainViewModel();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = model;
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //this.model.InfoModel.DPName = "change data";
            MessageBox.Show(this.model.InfoModel.DPName);
        }
    }
}
MainWindow
复制代码
复制代码
<Window x:Class="MVVMStudy.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:MVVMStudy"
        xmlns:vm="clr-namespace:MVVMStudy.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="400">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel>
            <TextBox Text="{Binding InfoModel.DPName,UpdateSourceTrigger=PropertyChanged ,Mode=TwoWay}" FontSize="20"/>
            <TextBox Text="" FontSize="20"/>
            <Button Content="button" Click="Button_Click"/>
        </StackPanel>
    </Grid>
</Window>
XMAL代码
复制代码

 

技术总结:

1、InfoModel必须继承DependencyObject,必须引用using System.Windows;

 

posted on   hanzq_go  阅读(157)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示