■ GridControl 클래스가 다른 스레드에서 갱신되는 데이터를 표시하는 방법을 보여준다.
▶ DataItem.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 |
using System.Collections.ObjectModel; using System.ComponentModel; namespace TestProject { /// <summary> /// 데이터 항목 /// </summary> public class DataItem : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 데이터 컬렉션 /// </summary> private static ObservableCollection<DataItem> _collection; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 값 /// </summary> private volatile int value; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 데이터 항목 컬렉션 - Collection /// <summary> /// 데이터 항목 컬렉션 /// </summary> public static ObservableCollection<DataItem> Collection { get { if(_collection == null) { _collection = new ObservableCollection<DataItem>(); for(int i = 0; i < 100; i++) { _collection.Add(new DataItem() { Name = "Name" + i.ToString(), Value = i }); } } return _collection; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #endregion #region 값 - Value /// <summary> /// 값 /// </summary> public int Value { get { return value; } set { this.value = value; OnPropertyChanged("Value"); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return Name; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 속성 변경시 처리하기 - OnPropertyChanged(propertyName) /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="propertyName">속성명</param> private void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } } |
▶ ViewModel.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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Threading; namespace TestProject { /// <summary> /// 뷰 모델 /// </summary> public class ViewModel : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region (INotifyPropertyChanged) 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion #region 비동기 처리 시작시 - AsyncProcessingStarted /// <summary> /// 비동기 처리 시작시 /// </summary> public event EventHandler AsyncProcessingStarted; #endregion #region 비동기 처리 완료시 - AsyncProcessingCompleted /// <summary> /// 비동기 처리 완료시 /// </summary> public event EventHandler AsyncProcessingCompleted; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 잠금자 /// </summary> private static object locker = new object(); /// <summary> /// 타이머 /// </summary> private Timer timer; /// <summary> /// 난수 발생기 /// </summary> private Random tandom = new Random(); /// <summary> /// 데이터 항목 컬렉션 /// </summary> private ObservableCollection<DataItem> collection; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 데이터 항목 컬렉션 - Collection /// <summary> /// 데이터 항목 컬렉션 /// </summary> public ObservableCollection<DataItem> Collection { get { return this.collection; } set { if(this.collection == value) { return; } this.collection = value; OnPropertyChanged("Collection"); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ViewModel() /// <summary> /// 생성자 /// </summary> public ViewModel() { this.collection = DataItem.Collection; this.timer = new Timer(TimerCallback, null, 1000, 1000); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 속성 변경시 처리하기 - OnPropertyChanged(propertyName) /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="propertyName">속성명</param> private void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion #region 비동기 처리 시작시 처리하기 - OnAsyncProcessingStarted() /// <summary> /// 비동기 처리 시작시 처리하기 /// </summary> private void OnAsyncProcessingStarted() { AsyncProcessingStarted?.Invoke(this, EventArgs.Empty); } #endregion #region 비동기 처리 완료시 처리하기 - OnAsyncProcessingCompleted() /// <summary> /// 비동기 처리 완료시 처리하기 /// </summary> private void OnAsyncProcessingCompleted() { AsyncProcessingCompleted?.Invoke(this, EventArgs.Empty); } #endregion #region 타이머 콜백 처리하기 - TimerCallback(state) /// <summary> /// 타이머 콜백 처리하기 /// </summary> /// <param name="state">상태</param> private void TimerCallback(object state) { lock(locker) { OnAsyncProcessingStarted(); foreach(DataItem item in Collection) { item.Value = tandom.Next(100); } OnAsyncProcessingCompleted(); } } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<Window x:Class="TestProject.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" Width="800" Height="600" Title="GridControl 클래스 : 다른 스레드에서 갱신되는 데이터 표시하기" FontFamily="나눔고딕코딩" FontSize="16"> <dxg:GridControl x:Name="gridControl" ItemsSource="{Binding Collection}" AutoGenerateColumns="AddNew"> </dxg:GridControl> </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 |
using System; using System.Windows; using System.Windows.Threading; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 뷰 모델 /// </summary> private ViewModel viewModel; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.viewModel = new ViewModel(); this.viewModel.AsyncProcessingStarted += viewModel_AsyncProcessingStarted; this.viewModel.AsyncProcessingCompleted += viewModel_AsyncProcessingCompleted; DataContext = this.viewModel; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 뷰 모델 비동기 처리 시작시 처리하기 - viewModel_AsyncProcessingStarted(sender, e) /// <summary> /// 뷰 모델 비동기 처리 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void viewModel_AsyncProcessingStarted(object sender, EventArgs e) { this.gridControl.Dispatcher.Invoke(new Action(delegate { this.gridControl.BeginDataUpdate(); }), DispatcherPriority.DataBind); } #endregion #region 뷰 모델 비동기 처리 완료시 처리하기 - viewModel_AsyncProcessingCompleted(sender, e) /// <summary> /// 뷰 모델 비동기 처리 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void viewModel_AsyncProcessingCompleted(object sender, EventArgs e) { this.gridControl.Dispatcher.Invoke(new Action(delegate { this.gridControl.EndDataUpdate(); }), DispatcherPriority.DataBind); } #endregion } } |