■ GridControl 클래스의 SaveLayoutToStream/RestoreLayoutFromStream 메소드를 사용해 레이아웃을 저장하고 복구하는 방법을 보여준다.
▶ Issue.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
namespace TestProject { /// <summary> /// 이슈 /// </summary> public class Issue { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이슈명 - IssueName /// <summary> /// 이슈명 /// </summary> public string IssueName { get; set; } #endregion #region 이슈 타입 - IssueType /// <summary> /// 이슈 타입 /// </summary> public string IssueType { get; set; } #endregion #region 개인 여부 - IsPrivate /// <summary> /// 개인 여부 /// </summary> public bool IsPrivate { get; set; } #endregion } } |
▶ IssueList.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System.Collections.Generic; namespace TestProject { /// <summary> /// 이슈 리스트 /// </summary> public class IssueList { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 데이터 구하기 - GetData() /// <summary> /// 데이터 구하기 /// </summary> /// <returns>이슈 리스트</returns> public List<Issue> GetData() { List<Issue> list = new List<Issue> { new Issue() { IssueName = "Transaction History" , IssueType = "Bug" , IsPrivate = true }, new Issue() { IssueName = "Ledger: Inconsistency", IssueType = "Bug" , IsPrivate = false }, new Issue() { IssueName = "Data Import" , IssueType = "Request", IsPrivate = false }, new Issue() { IssueName = "Data Archiving" , IssueType = "Request", IsPrivate = true } }; return pList; } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Width="800" Height="600" Title="그리드 레이아웃을 메모리 스트림에 저장하고 복구하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <dxg:GridControl x:Name="gridControl" dx:DXSerializer.SerializationID="gridControl" dx:DXSerializer.StoreLayoutMode="All" dxg:GridSerializationOptions.AddNewColumns="False" dxg:GridSerializationOptions.RemoveOldColumns="False"> <dxg:GridControl.Columns> <dxg:GridColumn x:Name="issueNameGridColumn" FieldName="IssueName" Header="Issue Name" /> <dxg:GridColumn x:Name="issueTypeGridColumn" FieldName="IssueType" Header="Issue Type" /> <dxg:GridColumn x:Name="isPrivateGridColumn" FieldName="IsPrivate" Header="Private" /> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView /> </dxg:GridControl.View> </dxg:GridControl> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button Margin="0 5 0 0" Padding="3" Content="Add New Column" Click="addNewColumnButton_Click" /> <Button Margin="5 5 0 0" Padding="3" Content="Save Layout" Click="saveLayoutButton_Click" /> <Button x:Name="restoreLayoutButton" Margin="5 5 0 0" Padding="3" Click="restoreLayoutButton_Click" Content="Restore Layout" IsEnabled="{Binding IsLayoutSaved}" /> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
using System.IO; using System.Windows; using DevExpress.Xpf.Grid; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 레이아웃 저장 여부 속성 /// </summary> public static readonly DependencyProperty IsLayoutSavedProperty = DependencyProperty.Register ( "IsLayoutSaved", typeof(bool), typeof(Window), null ); #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 레이아웃 메모리 스트림 /// </summary> private MemoryStream layoutMemoryStream; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 레이아웃 저장 여부 - IsLayoutSaved /// <summary> /// 레이아웃 저장 여부 /// </summary> public bool IsLayoutSaved { get { return (bool)GetValue(IsLayoutSavedProperty); } set { SetValue(IsLayoutSavedProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { DataContext = this; InitializeComponent(); IsLayoutSaved = false; this.gridControl.ItemsSource = new IssueList().GetData(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 신규 컬럼 추가하기 버튼 클릭시 처리하기 - addNewColumnButton_Click(sender, e) /// <summary> /// 신규 컬럼 추가하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addNewColumnButton_Click(object sender, RoutedEventArgs e) { this.gridControl.Columns.Add(new GridColumn() { FieldName = "IsPrivate" }); } #endregion #region Save Layout 버튼 클릭시 처리하기 - saveLayoutButton_Click(sender, e) /// <summary> /// Save Layout 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveLayoutButton_Click(object sender, RoutedEventArgs e) { if(this.layoutMemoryStream != null) { this.layoutMemoryStream.Dispose(); this.layoutMemoryStream = null; } this.layoutMemoryStream = new MemoryStream(); this.gridControl.SaveLayoutToStream(this.layoutMemoryStream); IsLayoutSaved = true; } #endregion #region Restore Layout 버튼 클릭시 처리하기 - restoreLayoutButton_Click(sender, e) /// <summary> /// Restore Layout 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void restoreLayoutButton_Click(object sender, RoutedEventArgs e) { this.layoutMemoryStream.Position = 0; this.gridControl.RestoreLayoutFromStream(this.layoutMemoryStream); } #endregion } } |
▶ MainApplication.xaml
1 2 3 4 5 6 7 |
<Application x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> </Application> |
▶ MainApplication.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : Application { } } |