实例学习WPF的MVVM编程模式2

版权声明:转载请标明出处 https://blog.csdn.net/u012366767/article/details/81946387

软件功能:实现两个数据求和,通过两个Slider确定输入值,将结果显示到第三个Slider。

  •  NotificationObject.cs
using System.ComponentModel;

namespace MVVMDemo2.ViewModels
{
    class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
  • DelegateCommand.cs
using System;
using System.Windows.Input;

namespace MVVMDemo2
{
    class DelegateCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            if (CanExecuteFunc == null)
            {
                return true;
            }
            return CanExecuteFunc(parameter);
        }

        public void Execute(object parameter)
        {
            if (ExecuteAction == null)
            {
                return;
            }
            ExecuteAction(parameter);
        }

        public Action<object> ExecuteAction { get; set; }
        public Func<object, bool> CanExecuteFunc { get; set; }
    }
}
  • MainWindowViewModel.cs
using Microsoft.Win32;
using System;

namespace MVVMDemo2.ViewModels
{
    class MainWindowViewModel : NotificationObject
    {
        private double input1;

        public double Input1
        {
            get { return input1; }
            set
            {
                input1 = value;
                RaisePropertyChanged("Input1");
            }
        }

        private double input2;

        public double Input2
        {
            get { return input2; }
            set
            {
                input2 = value;
                RaisePropertyChanged("Input2");
            }
        }

        private double result;

        public double Result
        {
            get { return result; }
            set
            {
                result = value;
                RaisePropertyChanged("Result");
            }
        }

        public DelegateCommand AddCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }

        private void Add(object parameter)
        {
            Result = Input1 + Input2;
        }

        private void Save(object parameter)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.ShowDialog();
        }

        public MainWindowViewModel()
        {
            AddCommand = new DelegateCommand()
            {
                ExecuteAction = new Action<object>(Add),
            };

            SaveCommand = new DelegateCommand()
            {
                ExecuteAction = new Action<object>(Save),
            };
        }
    }
}
  • 主xaml文件
<Window x:Class="MVVMDemo2.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:MVVMDemo2"
        mc:Ignorable="d"
        Title="MainWindow" Height="250" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Menu>
            <MenuItem Header="_File">
                <MenuItem Header="_Save" Command="{Binding SaveCommand}"/>
            </MenuItem>
        </Menu>
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Slider Grid.Row="0" Minimum="-100" Maximum="100" Margin="4" Value="{Binding Input1}"/>
            <Slider Grid.Row="1" Minimum="-100" Maximum="100" Margin="4" Value="{Binding Input2}"/>
            <Slider Grid.Row="2" Minimum="-100" Maximum="100" Margin="4" Value="{Binding Result}"/>
            <Button Grid.Row="3" Content="Add" Width="120" Height="80" Command="{Binding AddCommand}"/>
        </Grid>
    </Grid>
</Window>
  • 后台代码
using MVVMDemo2.ViewModels;
using System.Windows;

namespace MVVMDemo2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }
}
  • 完整代码

https://download.csdn.net/download/u012366767/10620094

猜你喜欢

转载自blog.csdn.net/u012366767/article/details/81946387