标签搜索
C#

WPF MVVM手敲简单框架

Y
Y
2020-05-18 / 0 评论 / 89 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年05月18日,已超过1222天没有更新,若内容或图片失效,请留言反馈。

好处是,XAML界面Binding到数据后,只需要更新数据,无需再次手动绑定即可完成界面更新。

1.创捷数据类,Model。
例如我创建的CarInfo.cs

namespace MVVMDemo1
{
    public class CarInfo
    {
        string CarName_;
        string CarID_;

        public string CarName
        {
            get { return CarName_; }
            set { CarName_ = value; }
        }

        public string CarID
        {
            get { return CarID_; }
            set { CarID_ = value; }
        }
    }
}

2.创建视图数据类,ViewModel。
例如我创建的CarInfoViewModel.cs

using System.ComponentModel;

namespace MVVMDemo1
{
    public class CarInfoViewModel : INotifyPropertyChanged
    {
        CarInfo CarInfo_;
        public CarInfoViewModel()
        {
            CarInfo_ = new CarInfo() {CarName="河南1",CarID="HMB001" };
        }

        public string CarName
        {
            get { return CarInfo_.CarName; }
            set { CarInfo_.CarName = value; RaisePropertyChanged("CarName"); }
        }
        public string CarID
        {
            get { return CarInfo_.CarID; }
            set { CarInfo_.CarID = value; RaisePropertyChanged("CarID"); }
        }

        /// <summary>
        /// 实现接口
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyInfo)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if(handler != null)
            {
                handler(this,new PropertyChangedEventArgs(propertyInfo));
            }
        }
    }
}

3.视图界面声明视图模型
Xaml文件中声明视图模型

<Window x:Class="MVVMDemo1.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:MVVMDemo1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <!--声明创建一个CarViewModel的实例-->
        <local:CarInfoViewModel/>
    </Window.DataContext>
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding CarName}" VerticalAlignment="Top" Width="120"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" Text="{Binding CarID}" VerticalAlignment="Top" Width="120"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="195,145,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

4.关联数据上下文
主界面后台关联上下文,实现自动刷新数据

using System.Windows;

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

        CarInfoViewModel carInfoViewModel;
        /// <summary>
        /// 视图模型
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            //已经在xaml代码中声明了视图模型实例,获取到引用因此我们可以在按钮click事件里使用它
            carInfoViewModel = base.DataContext as CarInfoViewModel;
        }
        /// <summary>
        /// 更改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //更改数据,界面实时更新
            carInfoViewModel.CarID = "";
        }
    }
}

End

0

评论 (0)

取消