• wpf中INotifyPropertyChanged的用法


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    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.Shapes;

    namespace WpfApplication1
    {
    /// <summary>
    /// Window9.xaml 的交互逻辑
    /// </summary>
    public partial class Window9 : Window
    {
    public Window9()
    {
    InitializeComponent();
    this.DataContext = new ViewModel();
    }
    }
    class NotificationObject : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
    if (this.PropertyChanged != null)
    {
    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    }
    }
    class DelegateCommand : ICommand
    {
    //A method prototype without return value.
    public Action<object> ExecuteCommand = null;
    //A method prototype return a bool type.
    public Func<object, bool> CanExecuteCommand = null;
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
    if (CanExecuteCommand != null)
    {
    return this.CanExecuteCommand(parameter);
    }
    else
    {
    return true;
    }
    }

    public void Execute(object parameter)
    {
    if (this.ExecuteCommand != null) this.ExecuteCommand(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
    if (CanExecuteChanged != null)
    {
    CanExecuteChanged(this, EventArgs.Empty);
    }
    }
    }
    class Model : NotificationObject
    {
    private string _wpf = "WPF";

    public string WPF
    {
    get { return _wpf; }
    set
    {
    _wpf = value;
    this.RaisePropertyChanged("WPF");
    }
    }

    public void Copy(object obj)
    {
    this.WPF += " WPF";
    }

    }
    class ViewModel
    {
    public DelegateCommand CopyCmd { get; set; }
    public Model model { get; set; }

    public ViewModel()
    {
    this.model = new Model();
    this.CopyCmd = new DelegateCommand();
    this.CopyCmd.ExecuteCommand = new Action<object>(this.model.Copy);
    }
    }

    }

    ________________________________

    <Window x:Class="WpfApplication1.Window9"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window9" Height="300" Width="300">
    <Grid>
    <StackPanel VerticalAlignment="Top">
    <TextBlock Text="{Binding model.WPF}" Height="208" TextWrapping="WrapWithOverflow"></TextBlock>
    <Button Command="{Binding CopyCmd}" Height="93" Width="232" Content="Copy" Margin="30,0"/>
    </StackPanel>
    </Grid>
    </Window>

  • 相关阅读:
    B树、B树、B+树、B*树
    CSS黑客技术的实现
    ORM映射框架总结SQL 语句生成组件
    突然发现 ViewState,Linq 水火不容
    ALinq 入门学习(一)ALinq简介
    Google 地图基本接口(一)
    ORM映射框架总结映射桥梁
    ALinq 入门学习(二)DataContext
    ORM映射框架总结数据库操作库(精修版)
    C# 使用线程你可能不知道的问题
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14301006.html
Copyright © 2020-2023  润新知