标签:
<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(); } } }
Saving Grid(Dev Express) Layout with MVVM(code from DevExpress support)
标签:
原文地址:http://www.cnblogs.com/burnedice/p/4567731.html