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

<Window x:Class="WPFGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
        xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
        xmlns:local="clr-namespace:WPFGridTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:VM/>
    </Window.DataContext>
    <Window.Resources>
        <local:SaveRestoreCommands x:Key="saveRestoreLayoutCommands" Path="1.xml"/>
    </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).SaveLayoutToXml(Path);
            if (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).RestoreLayoutFromXml(Path);
        }
    }
    public 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();
        }
    }
}
时间: 2024-09-03 23:57:01

Saving Grid(Dev Express) Layout with MVVM(code from DevExpress support)的相关文章

Dev Express 安装

Dev Express 安装 点击DevExpressUniversalTrialComplete-20151209.exe开始安装 选择需要安装的产品 选择需要安装的产品目录,这里设置为D盘 开始安装  等待安装完成,约为5分钟左右 完成提示帮助页面  安装完DevExpress程序后开始破解过程.双击[DevExpress.Patch 7.0-by dimaster]的程序包,弹出[DevExpress.Patch by dimaster]的窗体,直接点击[Apply patch]按钮就开始

Dev Express Report 学习总结(一) 基础知识总结

Dev Express,一个非常优秀的报表控件.像其他报表一样,该报表也包括几个主要部分:Report Header,Page Header,Group Header,Detail,Group Footer,Page Footer和Report Footer共7个部分.,除此之外,我们还可以添加Detail Report,并同时添加与此相对应的Report Header/Footer.借助此功能可以实现多个Heaer与多个Detail相互交叉的效果! 一. 常见总结 CalculatedFile

DEV Express

记录在使用DEV Express中遇到的所有问题及解决方案 问题1:将Dev11升级到Dev14 解决方案:将解决方案中原有Dev引用删除,重新添加必须的Dev14引用,问题解决: 问题2:LC.exe已退出,代码-1 原因:在升级dev组件时,产生的错误,组件错误 解决方案:删除properties文件夹下的License文件,重新生成,问题解决:

Dev Express之ImageComboBoxEdit,RepositoryItemImageComboBox使用方式

 Dev Express之ImageComboBoxEdit,RepositoryItemImageComboBox使用方式 1.使用ImageComboBoxEdit实现下拉框键值数据函数 public static void InitCmb(ImageComboBoxEdit cmb, DataTable source, string value, string text, bool isDefaultNull) { ImageComboBoxItem[] items = new Image

2016.1.19 DEV Express控件GirdControl使用

DEV控件在工具箱中消失处理方法 开始-->程序-->Developer Express v2009 vol 3(依据版本不同)-->Components-->Tools-->ToolboxCreator   1.点击一行选择完整一行 Run Designer->View->OptionsBehavior->EditorShowMode 设置为:Click Run Designer->View->OptionsSelection.EnableAp

DEV Express中NavBarCointrol的使用

尚未对内容进行删减,内容有偏差和冗余,谨慎阅读. 发现在后面,写在前面: 13,之前在Default模式下,之所以很多Appearance属性都起不到作用,是因为Control的LookAndFeel默认选择了UseDefultLookAndFeel. 14,最早在接触该控件的时候,找了很久上调下拉的按钮是怎么出来的,可以设置Control的SkinExplorerBarViewScrollStyle为Button或者是Scroll,即按钮还是滚动条. 一.Control 1,NavBarCon

DEV Express中Bar Manager的使用

未排版 在barManager中可以添加多种元素,如皮肤按钮,复选框等,但是下拉菜单却给出了多个冗余的控件. 遗留问题:怎么设置Bar为大图标,查找是否存在Ribbon控件. Bar 1,        BarManager位于“导航和布局”分类中,将其拖动到窗体后,默认生成bar1/bar2/bar3三条,分别对应于菜单栏/工具栏和任务栏,可以拖动Bar停靠在父容器的上/下/左/右,或者浮动. Bar本身不存在快速便捷的界面. Bar的CanDockStyle属性标志Bar允许的停靠方式. B

DEV express 对Gridview某行的元素赋值

1:获取选中的行某列的值 string colValue= this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[1]).ToString() ; gridview.GetFocusedRowCellValue("字段 的值") DEV GRIDVIEW小常识 1.设置Gridview控件的某列不可编辑 this.gridData.gridView1.Columns[

WPF MVVM CODE MAP