星空网 > 软件开发 > ASP.net

Saving Grid(Dev Express) Layout with MVVM(code from DevExpress support)

<Window x:Class="WPFGridTest.MainWindow"    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    ="http://schemas.microsoft.com/winfx/2006/xaml"    ="http://schemas.devexpress.com/winfx/2008/xaml/grid"    ="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"    ="http://schemas.devexpress.com/winfx/2008/xaml/editors"    ="http://schemas.devexpress.com/winfx/2008/xaml/core"    ="http://schemas.devexpress.com/winfx/2008/xaml/bars"    ="http://schemas.microsoft.com/expression/2010/interactivity"    ="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"    ="clr-namespace:WPFGridTest"    Title="MainWindow" Height="350" Width="525">  <Window.DataContext>    <local:VM/>  </Window.DataContext>  <Window.Resources>    <local:SaveRestoreCommands x:Key="saveRestoreLayoutCommands" Path="1./>  </Window.Resources>  <Grid>    <Grid.RowDefinitions>      <RowDefinition Height="Auto"/>      <RowDefinition Height="*"/>    </Grid.RowDefinitions>    <StackPanel Orientation="Horizontal">      <Button Content="Save" Command="{Binding Path=SaveLayoutCommand, Source={StaticResource saveRestoreLayoutCommands}}" CommandParameter="{Binding ElementName= g}"/>      <Button Content="Restore" Command="{Binding Path=RestoreLayoutCommand, Source={StaticResource saveRestoreLayoutCommands}}" CommandParameter="{Binding ElementName=g}"/>    </StackPanel>    <dxg:GridControl x:Name="g" Grid.Row="1" ItemsSource="{Binding Source}">      <dxg:GridControl.View>        <dxg:TableView x:Name="v"/>      </dxg:GridControl.View>      <dxg:GridControl.Columns>        <dxg:GridColumn x:Name="c1" FieldName="ID"/>        <dxg:GridColumn x:Name="c2" FieldName="Value"/>      </dxg:GridControl.Columns>    </dxg:GridControl>  </Grid></Window>

using System;using System.Collections.Generic;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.Navigation;using System.Windows.Shapes;using DevExpress.Xpf.Grid;using DevExpress.Xpf.Core.Native;using DevExpress.Xpf.Bars;using System.ComponentModel;using System.Windows.Interactivity;using DevExpress.Xpf.Core;using System.ComponentModel.DataAnnotations;using System.Data;using System.Collections.ObjectModel;using DevExpress.Xpf.Editors;using DevExpress.Data;using DevExpress.Xpf.Editors.Settings;using DevExpress.Xpf.Core.Commands;using System.IO;namespace WPFGridTest {  /// <summary>  /// Interaction logic for MainWindow.xaml  /// </summary>  public partial class MainWindow : Window {    public MainWindow() {      InitializeComponent();    }  }  public class VM {    public TestDataList Source { get; set; }    public VM() {      Source = TestDataList.Create();    }  }  public class TestDataList : ObservableCollection<TestDataItem> {    public static TestDataList Create() {      TestDataList res = new TestDataList();      TestDataItem item = new TestDataItem();      item.ID = 0;      item.Value = "A";      res.Add(item);      item = new TestDataItem();      item.ID = 1;      item.Value = "b";      res.Add(item);      item = new TestDataItem();      item.ID = 2;      item.Value = "C";      res.Add(item);      item = new TestDataItem();      item.ID = 3;      item.Value = "C";      res.Add(item);      return res;    }  }  public class TestDataItem {    public int ID { get; set; }    public string Value { get; set; }  }  public class SaveRestoreLayoutCommandBase : ICommand {    internal string Path { get; set; }    public virtual bool CanExecute(object parameter) {      if (string.IsNullOrEmpty(Path)) return false;      return true;    }    public event EventHandler CanExecuteChanged;    public virtual void Execute(object parameter) {      if (!(parameter is GridControl)) throw new Exception();    }    internal void RaiseCanExecuteChanged() {      if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);    }  }  public class SaveLayoutCommand : SaveRestoreLayoutCommandBase {    public event EventHandler Executed;    public override void Execute(object parameter) {      base.Execute(parameter);      ((GridControl)parameter).SaveLayoutToif (Executed != null) Executed(this, EventArgs.Empty);    }  }  public class RestoreLayoutCommand : SaveRestoreLayoutCommandBase {    public override bool CanExecute(object parameter) {      if (!base.CanExecute(parameter) || !File.Exists(Path))        return false;      return true;    }    public override void Execute(object parameter) {      base.Execute(parameter);      ((GridControl)parameter).RestoreLayoutFrompublic class SaveRestoreCommands {    string path;    public string Path { get { return path; }      set {        path = value;        SaveLayoutCommand.Path = path;        RestoreLayoutCommand.Path = path;      }    }    public SaveLayoutCommand SaveLayoutCommand { get; private set; }    public RestoreLayoutCommand RestoreLayoutCommand { get; private set; }    public SaveRestoreCommands() {      SaveLayoutCommand = new SaveLayoutCommand() { Path = this.Path };      RestoreLayoutCommand = new RestoreLayoutCommand() { Path = this.Path };      SaveLayoutCommand.Executed +=new EventHandler(SaveLayoutCommand_Executed);    }    void SaveLayoutCommand_Executed(object sender, EventArgs e) {      if (RestoreLayoutCommand != null) RestoreLayoutCommand.RaiseCanExecuteChanged();    }  }}

 




原标题:Saving Grid(Dev Express) Layout with MVVM(code from DevExpress support)

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

财赋优:https://www.ikjzd.com/w/1573201029726904322
icc认证:https://www.ikjzd.com/w/1573201030259580929
iCraft Gifts:https://www.ikjzd.com/w/1573201043207741441
ICTI认证:https://www.ikjzd.com/w/1573201043811721218
Icyzone:https://www.ikjzd.com/w/1573201044557963265
艾瓦特:https://www.ikjzd.com/w/1573201059112542209
37号文今后是否会更新?一文详解关键信息 :https://www.kjdsnews.com/a/1836441.html
探讨内地人开设香港账户的可行性 :https://www.kjdsnews.com/a/1836442.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流