현재 데브익스프레스 18.2 버전을 사용중이지만 최신버전을 사용해도 상관없을거같습니다.
참고하실 수 있는 코드를 올려놓겠습니다.
MainWIndow.xaml
<Window x:Class="GridExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GridExample"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Width="500" Height="370">
<Window.DataContext>
<local:PersonsViewModel />
</Window.DataContext>
<!--<Window.Resources>
<dxg:TableView x:Key="TableView"/>
<dxg:CardView x:Key="CardView"/>
<dxg:TreeListView x:Key="TreeListView"/>
<Style TargetType="dxg:GridControl">
<Style.Triggers>
<DataTrigger Binding="{Binding CurrentView}">
<DataTrigger.Value>
<x:StaticExtension Member="local:GridViews.TableView"/>
</DataTrigger.Value>
<Setter Property="View" Value="{StaticResource TableView}"/>
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}">
<DataTrigger.Value>
<x:StaticExtension Member="local:GridViews.CardView"/>
</DataTrigger.Value>
<Setter Property="View" Value="{StaticResource CardView}"/>
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}">
<DataTrigger.Value>
<x:StaticExtension Member="local:GridViews.TreeView"/>
</DataTrigger.Value>
<Setter Property="View" Value="{StaticResource TreeListView}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>-->
<StackPanel>
<!--<dxe:ComboBoxEdit SelectedItem="{Binding CurrentView, Mode=TwoWay}"
IsTextEditable="False"
ItemsSource="{Binding Views}"
Width="150" Height="24" >
</dxe:ComboBoxEdit>
<dxg:GridControl Name="grid" Height="300"
DataSource="{Binding Persons}"
AutoPopulateColumns="True" >
</dxg:GridControl>-->
<local:Test/>
</StackPanel>
</Window>
Test.xaml
<UserControl x:Class="GridExample.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GridExample"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<dxg:TableView x:Key="TableView"/>
<dxg:CardView x:Key="CardView"/>
<dxg:TreeListView x:Key="TreeListView"/>
<Style TargetType="dxg:GridControl">
<Style.Triggers>
<DataTrigger Binding="{Binding CurrentView}">
<DataTrigger.Value>
<x:StaticExtension Member="local:GridViews.TableView"/>
</DataTrigger.Value>
<Setter Property="View" Value="{StaticResource TableView}"/>
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}">
<DataTrigger.Value>
<x:StaticExtension Member="local:GridViews.CardView"/>
</DataTrigger.Value>
<Setter Property="View" Value="{StaticResource CardView}"/>
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}">
<DataTrigger.Value>
<x:StaticExtension Member="local:GridViews.TreeView"/>
</DataTrigger.Value>
<Setter Property="View" Value="{StaticResource TreeListView}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<StackPanel>
<dxe:ComboBoxEdit SelectedItem="{Binding CurrentView, Mode=TwoWay}"
IsTextEditable="False"
ItemsSource="{Binding Views}"
Width="150" Height="24" >
</dxe:ComboBoxEdit>
<dxg:GridControl Name="grid" Height="300"
DataSource="{Binding Persons}"
AutoPopulateColumns="True" >
</dxg:GridControl>
</StackPanel>
</UserControl>
PersonDataSource.cs
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace GridExample
{
public enum GridViews { TableView, CardView, TreeView };
public class PersonsViewModel: ViewModelBase
{
ObservableCollection<Person> persons;
public PersonsViewModel() {
List<string> Names = new List<string> { "Alex", "Alice", "Tony", "Den", "Andrew", "John", "Donald", "Brian", "Effy", "Lisa", "Matthew" };
persons = new ObservableCollection<Person>();
for (int i = 0; i < 10; i++) {
persons.Add(new Person(Names[i], "Last name " + i, 21 + i, i % 2 == 0, 170 + i, 75 + i));
}
persons[5].Age = 22;
persons[8].Age = 50;
}
public List<GridViews> Views {
get {
return new List<GridViews>() { GridViews.TableView, GridViews.CardView, GridViews.TreeView };
}
}
GridViews view = GridViews.TableView;
public GridViews CurrentView {
get {
return view;
}
set {
view = value;
RaisePropertyChanged("CurrentView");
}
}
public ObservableCollection<Person> Persons {
get {
return persons;
}
}
[Command]
public void Card()
{
CurrentView = GridViews.CardView;
}
[Command]
public void Table()
{
CurrentView = GridViews.TableView;
}
}
public class Person : INotifyPropertyChanged, IDataErrorInfo {
const string lastNamePropertyName = "LastName";
const string firstNamePropertyName = "FirstName";
const string agePropertyName = "Age";
const string heightPropertyName = "IsMarried";
const string isMarriedPropertyName = "Height";
const string weightPtropertyName = "Weight";
string firstName;
string lastName;
int age;
bool isMarried;
int height;
int weight;
PersonPropertiesValidator Validator = new PersonPropertiesValidator();
public Person(string firstName, string lastName, int age, bool isMarried, int height, int weight) {
FirstName = firstName;
LastName = lastName;
Age = age;
IsMarried = isMarried;
Weight = weight;
Height = height;
}
public string FirstName {
get {
return firstName;
}
set {
Validator.IsNameValid(value, firstNamePropertyName);
firstName = value;
RaisePropertyChanged(firstNamePropertyName);
}
}
public string LastName {
get {
return lastName;
}
set {
Validator.IsNameValid(value, lastNamePropertyName);
lastName = value;
RaisePropertyChanged(lastNamePropertyName);
}
}
public int Age {
get {
return age;
}
set {
Validator.IsAgeValid(value, agePropertyName);
age = value;
RaisePropertyChanged(agePropertyName);
}
}
public bool IsMarried {
get {
return isMarried;
}
set {
isMarried = value;
RaisePropertyChanged(isMarriedPropertyName);
}
}
public int Height {
get {
return height;
}
set {
height = value;
RaisePropertyChanged(heightPropertyName);
}
}
public int Weight {
get {
return weight;
}
set {
weight = value;
RaisePropertyChanged(weightPtropertyName);
}
}
#region INotifyPropertyChanged members
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region IDataErrorInfo members
public string Error {
get {
return Validator.DataError;
}
}
public string this[string columnName] {
get {
if (Validator.DataErrors.ContainsKey(columnName))
return Validator.DataErrors[columnName];
else
return null;
}
}
#endregion
}
public class PersonPropertiesValidator {
string dataError = "";
Dictionary<string, string> dataErrors = new Dictionary<string, string>();
public bool IsNameValid(string value, string propertyName) {
if (string.IsNullOrEmpty(value)) {
dataErrors[propertyName] = "Full name is required.";
return false;
} else {
ClearPropertyErrors(propertyName);
return true;
}
}
public bool IsAgeValid(int value, string propertyName) {
if (value <= 0) {
dataErrors[propertyName] = "Age validation failed.";
return false;
} else {
ClearPropertyErrors(propertyName);
return true;
}
}
public string DataError {
get {
return dataError;
}
}
public Dictionary<string, string> DataErrors {
get {
return dataErrors;
}
}
void ClearPropertyErrors(string propertyName) {
if (dataErrors.ContainsKey(propertyName))
dataErrors.Remove(propertyName);
}
}
}