zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    Codeforces 1009F Dominant Indices
    C++之++运算符重载问题
    Codeforces 1010D Mars rover
    这是一个开始
    MoreEffectiveC++Item35(异常)(条款9-15)
    C++隐式类类型转化
    MoreEffectiveC++Item35(操作符)(条款5-8)
    MoreEffectiveC++Item35(基础议题)(条款1-4)
    php+mysql网站无限级栏目分类-递归获取树形结构函数
    JavaScript简易动画
  • 原文地址:https://www.cnblogs.com/hanzq/p/14718749.html
Copyright © 2011-2022 走看看