■ LookUpEdit 클래스에서 신규 값을 처리하는 방법을 보여준다.
▶ ProductControl.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 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 |
<UserControl x:Class="TestProject.ProductControl" 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:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" Width="300" Height="280" FontFamily="나눔고딕코딩" FontSize="12"> <Grid> <StackPanel Margin="10"> <StackPanel HorizontalAlignment="Center" Orientation="Horizontal" Margin="0 0 0 10"> <TextBlock VerticalAlignment="Center" Text="Product Name :" /> <dxe:TextEdit Margin="5 0 0 0" VerticalAlignment="Center" IsReadOnly="True" EditValue="{Binding Path=ProductName, Mode=OneWay}" /> </StackPanel> <dx:GroupFrame MinWidth="300" Header="New Value Properties"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" Text="Country : " /> <dxe:TextEdit x:Name="countryTextEdit" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="5" Width="150" Height="25" EditValue="{Binding Path=Country, Mode=TwoWay}" /> <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" Text="City : " /> <dxe:TextEdit x:Name="cityTextEdit" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Margin="5" Width="150" Height="25" EditValue="{Binding Path=City, Mode=TwoWay}" /> <TextBlock Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" Text="Quantity : " /> <dxe:TextEdit x:Name="quantityTextEdit" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" Margin="5" Width="150" Height="25" EditValue="{Binding Path=Quantity, Mode=TwoWay}" /> <TextBlock Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" Text="Unit Price : " /> <dxe:SpinEdit x:Name="unitPriceSpinEdit" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" Margin="5" Width="150" Height="25" EditValue="{Binding Path=UnitPrice, Mode=TwoWay, Converter={StaticResource StringToIntegerValueConverterKey}}" /> <TextBlock Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" Text="Product ID : " /> <dxe:SpinEdit x:Name="idSpinEdit" Grid.Row="4" Grid.Column="1" VerticalAlignment="Center" Margin="5" Width="150" Height="25" EditValue="{Binding Path=ID, Mode=TwoWay, Converter={StaticResource StringToIntegerValueConverterKey}}" /> </Grid> </dx:GroupFrame> </StackPanel> </Grid> </UserControl> |
▶ ProductControl.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 |
using System.Windows.Controls; namespace TestProject { /// <summary> /// 제품 컨트롤 /// </summary> public partial class ProductControl : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ProductControl() /// <summary> /// 생성자 /// </summary> public ProductControl() { InitializeComponent(); } #endregion } } |
▶ ProductListViewModel.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 |
using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows.Input; using DevExpress.Xpf.Mvvm; namespace TestProject { /// <summary> /// 제품 리스트 뷰 모델 /// </summary> public class ProductListViewModel : ViewModelBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 제품 컬렉션 /// </summary> private ObservableCollection<Product> productCollection = new ObservableCollection<Product>(); /// <summary> /// 제품 폼 보여주기 명령 /// </summary> private ICommand showProductFormCommand; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제품 컬렉션 - ProductCollection /// <summary> /// 제품 컬렉션 /// </summary> public ObservableCollection<Product> ProductCollection { get { return this.productCollection; } } #endregion #region 제품 폼 보여주기 명령 - ShowProductFormCommand /// <summary> /// 제품 폼 보여주기 명령 /// </summary> public ICommand ShowProductFormCommand { get { if(this.showProductFormCommand == null) { this.showProductFormCommand = new DelegateCommand<string>(OnShowProductFormCommandExecute); } return this.showProductFormCommand; } } #endregion #region 대화 상자 서비스 - DialogService /// <summary> /// 대화 상자 서비스 /// </summary> public IDialogService DialogService { get { return GetService<IDialogService>(); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ProductListViewModel() /// <summary> /// 생성자 /// </summary> public ProductListViewModel() : base() { this.productCollection.Add(new Product() { ID = 1000, ProductName = "Chang" , Country = "UK" , City = "Cowes" , UnitPrice = 19, Quantity = 10 }); this.productCollection.Add(new Product() { ID = 1001, ProductName = "Gravad lax" , Country = "Italy" , City = "Reggio Emilia" , UnitPrice = 12, Quantity = 16 }); this.productCollection.Add(new Product() { ID = 1002, ProductName = "Ravioli Angelo", Country = "Brazil" , City = "Rio de Janeiro", UnitPrice = 19, Quantity = 12 }); this.productCollection.Add(new Product() { ID = 1003, ProductName = "Tarte au sucre", Country = "Germany", City = "QUICK-Stop" , UnitPrice = 22, Quantity = 50 }); this.productCollection.Add(new Product() { ID = 1004, ProductName = "Steeleye Stout", Country = "USA" , City = "Reggio Emilia" , UnitPrice = 18, Quantity = 20 }); this.productCollection.Add(new Product() { ID = 1005, ProductName = "Pavlova" , Country = "Austria", City = "Graz" , UnitPrice = 21, Quantity = 52 }); this.productCollection.Add(new Product() { ID = 1006, ProductName = "Longlife Tofu" , Country = "USA" , City = "Boise" , UnitPrice = 7 , Quantity = 120}); this.productCollection.Add(new Product() { ID = 1007, ProductName = "Alice Mutton" , Country = "Mexico" , City = "Meéxico D.F." , UnitPrice = 21, Quantity = 15 }); this.productCollection.Add(new Product() { ID = 1008, ProductName = "Alice Mutton" , Country = "Canada" , City = "Tsawwassen" , UnitPrice = 44, Quantity = 16 }); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 제품 폼 보여주기 명령 실행시 처리하기 - OnShowProductFormCommandExecute(parameter) /// <summary> /// 제품 폼 보여주기 명령 실행시 처리하기 /// </summary> /// <param name="parameter">매개 변수</param> private void OnShowProductFormCommandExecute(string parameter) { if(string.IsNullOrEmpty(parameter)) { return; } Product product = new Product() { ProductName = parameter }; UICommand addProductUICommand = new UICommand() { Caption = "Add", IsCancel = false, IsDefault = true }; UICommand cancelProductUICommand = new UICommand() { Caption = "Cancel", IsDefault = false, IsCancel = true }; UICommand resultUICommand = DialogService.ShowDialog(new List<UICommand>() { addProductUICommand, cancelProductUICommand}, "Add New Product", product); if(resultUICommand == addProductUICommand) { this.productCollection.Add(product); } } #endregion } } |
▶ ProductNameEventArgsConverter.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 |
using DevExpress.Xpf.Editors; using DevExpress.Xpf.Mvvm.UI; namespace TestProject { /// <summary> /// 제품명 이벤트 인자 변환자 /// </summary> public class ProductNameEventArgsConverter : IEventArgsConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(e) /// <summary> /// 변환하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <returns>변환 값</returns> public object Convert(object e) { string productName = (e as ProcessNewValueEventArgs).DisplayText; return productName; } #endregion } } |
▶ StringToIntegerValueConverter.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 |
using System; using System.Windows.Data; using System.Globalization; namespace TestProject { /// <summary> /// 문자↔정수 값 변환자 /// </summary> public class StringToIntegerValueConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(value, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="value">값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">CultureInfo 객체</param> /// <returns>변환 값</returns> public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo) { return (int)value; } #endregion #region 역변환하기 - ConvertBack(value, targetType, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="value">값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">CultureInfo 객체</param> /// <returns>역변환 값</returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cultureInfo) { return value.ToString(); } #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 54 55 56 57 58 59 |
<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" xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="LookUpEdit 클래스 : 신규 값 처리하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.DataContext> <local:ProductListViewModel /> </Window.DataContext> <Grid> <dxg:LookUpEdit x:Name="lookUpEdit" ItemsSource="{Binding Path=ProductCollection}" Width="200" Height="25" ValueMember="ID" DisplayMember="ProductName" AddNewButtonPlacement="EditBox" AutoComplete="True" IncrementalFiltering="True" ImmediatePopup="True" AutoPopulateColumns="True" IsPopupAutoWidth="False"> <dxmvvm:Interaction.Triggers> <dxmvvm:EventToCommand PassEventArgsToCommand="True" Command="{Binding ShowProductFormCommand}" EventName="ProcessNewValue" SourceName="lookUpEdit"> <dxmvvm:EventToCommand.EventArgsConverter> <local:ProductNameEventArgsConverter /> </dxmvvm:EventToCommand.EventArgsConverter> </dxmvvm:EventToCommand> </dxmvvm:Interaction.Triggers> </dxg:LookUpEdit> </Grid> <dxmvvm:Interaction.Behaviors> <dx:DialogService DialogWindowStartupLocation="CenterOwner"> <dx:DialogService.ViewTemplate> <DataTemplate> <local:ProductControl /> </DataTemplate> </dx:DialogService.ViewTemplate> <dx:DialogService.DialogStyle> <Style TargetType="dx:DXDialogWindow"> <Setter Property="Width" Value="340" /> <Setter Property="Height" Value="340" /> </Style> </dx:DialogService.DialogStyle> </dx:DialogService> </dxmvvm:Interaction.Behaviors> </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 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion } } |