WPF实现一个控件编辑状态的撤销和前进功能,适用于任何具有编辑功能的场景

一·需要用到的特性如下:

        1,数据绑定;2,列表容器的深拷贝;3,路由命令;4,MVVM思想

二·下面我用一个可编辑数据的DataGrid作为示范:

     1. 首先,我们在xaml中创建一个DataGrid控件,控件的的数据源绑定在ViewModel中

<Window x:Name="window"
        x:Class="MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"      xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
        xmlns:sys ="clr-namespace:System;assembly=mscorlib"       
        Title="我爱你" 
>
<DataGrid x:Name="Grid" Margin="0,0,18,0" ItemsSource="{Binding CurrentViewModel.CurrentList, ElementName=window, Mode=TwoWay, UpdateSourceTrigger= PropertyChanged BeginningEdit = "DataGrid_BeginningEdit"}" /></window>

       2.当然数据源绑定的操作也可以在后台代码中进行,因为后台中也会进行路由命令的绑定操作:

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

        public MainWindow()
        {
            InitializeComponent();
        }

//将ViewModel类作为Window的依赖属性
        public static readonly DependencyProperty CurrentViewModelProperty = DependencyProperty.Register("CurrentViewModel", typeof(ViewModel), typeof(MainWindow),
                new PropertyMetadata(new ViewModel()));

        public ViewModel CurrentViewModel
        {
            get { return (ViewModel)GetValue(CurrentView
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邓琳律师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值