<Application x:Class="HowToCreateLookUpEdit.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HowToCreateLookUpEdit"
xmlns:viewmodel="clr-namespace:HowToCreateLookUpEdit.ViewModel"
StartupUri="MainWindow.xaml">
<Application.Resources>
<viewmodel:StringToIntConverter x:Key="converter" />
</Application.Resources>
</Application>
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Data
Imports System.Windows.Input
Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.Editors
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.UI
Namespace HowToCreateLookUpEdit.ViewModel
Public Class ProductListViewModel
Inherits ViewModelBase
Private products_Renamed As New ObservableCollection(Of Product)()
Public ReadOnly Property Products() As ObservableCollection(Of Product)
Get
Return products_Renamed
End Get
End Property
Public Sub New()
MyBase.New()
products_Renamed.Add(New Product() With {.ProductName = "Chang", .Country = "UK", .City = "Cowes", .UnitPrice = 19, .Quantity = 10, .ID = 1000})
products_Renamed.Add(New Product() With {.ProductName = "Gravad lax", .Country = "Italy", .City = "Reggio Emilia", .UnitPrice = 12, .Quantity = 16, .ID = 1001})
products_Renamed.Add(New Product() With {.ProductName = "Ravioli Angelo", .Country = "Brazil", .City = "Rio de Janeiro", .UnitPrice = 19, .Quantity = 12, .ID = 1002})
products_Renamed.Add(New Product() With {.ProductName = "Tarte au sucre", .Country = "Germany", .City = "QUICK-Stop", .UnitPrice = 22, .Quantity = 50, .ID = 1003})
products_Renamed.Add(New Product() With {.ProductName = "Steeleye Stout", .Country = "USA", .City = "Reggio Emilia", .UnitPrice = 18, .Quantity = 20, .ID = 1004})
products_Renamed.Add(New Product() With {.ProductName = "Pavlova", .Country = "Austria", .City = "Graz", .UnitPrice = 21, .Quantity = 52, .ID = 1005})
products_Renamed.Add(New Product() With {.ProductName = "Longlife Tofu", .Country = "USA", .City = "Boise", .UnitPrice = 7, .Quantity = 120, .ID = 1006})
products_Renamed.Add(New Product() With {.ProductName = "Alice Mutton", .Country = "Mexico", .City = "México D.F.", .UnitPrice = 21, .Quantity = 15, .ID = 1007})
products_Renamed.Add(New Product() With {.ProductName = "Alice Mutton", .Country = "Canada", .City = "Tsawwassen", .UnitPrice = 44, .Quantity = 16, .ID = 1008})
End Sub
Public _ShowProductFormCommand As ICommand
Public ReadOnly Property ShowProductFormCommand() As ICommand
Get
If _ShowProductFormCommand Is Nothing Then
_ShowProductFormCommand = New DelegateCommand(Of String)(AddressOf OnShowProductFormCommandExecute)
End If
Return _ShowProductFormCommand
End Get
End Property
Private ReadOnly Property DialogService() As IDialogService
Get
Return GetService(Of IDialogService)()
End Get
End Property
Private Sub OnShowProductFormCommandExecute(ByVal parameter As String)
If parameter = String.Empty Then
Return
End If
Dim p As New Product() With {.ProductName = parameter}
Dim addProductCommand As New UICommand() With {.Caption = "Add", .IsCancel = False, .IsDefault = True}
Dim cancelProductCommand As New UICommand() With {.Caption = "Cancel", .IsDefault = False, .IsCancel = True}
Dim result As UICommand = DialogService.ShowDialog(dialogCommands:= New List(Of UICommand)() From {addProductCommand, cancelProductCommand}, title:= "Add new row", viewModel:= p)
If result Is addProductCommand Then
products_Renamed.Add(p)
End If
End Sub
End Class
Public Class ProductListValueConverter
Implements IEventArgsConverter
Public Function Convert(ByVal sender As Object, ByVal args As Object) As Object Implements IEventArgsConverter.Convert
Dim productName As String = DirectCast(args, ProcessNewValueEventArgs).DisplayText
Return productName
End Function
End Class
Public Class StringToIntConverter
Implements IValueConverter
#Region "IValueConverter Members"
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Return DirectCast(value, Integer)
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Return value.ToString()
End Function
#End Region
End Class
End Namespace
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports DevExpress.Mvvm
Namespace HowToCreateLookUpEdit.ViewModel
Public Class Product
Public Property ID() As Integer
Public Property ProductName() As String
Public Property Country() As String
Public Property City() As String
Public Property UnitPrice() As Integer
Public Property Quantity() As Integer
End Class
End Namespace
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.Grid.LookUp
Imports HowToCreateLookUpEdit.ViewModel
Namespace HowToCreateLookUpEdit
''' <summary>
''' Interaction logic for MainWindow.xaml
''' </summary>
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
End Class
End Namespace
<Window x:Class="HowToCreateLookUpEdit.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:local="clr-namespace:HowToCreateLookUpEdit"
xmlns:viewmodel="clr-namespace:HowToCreateLookUpEdit.ViewModel"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core">
<Window.DataContext>
<viewmodel:ProductListViewModel/>
</Window.DataContext>
<Grid>
<dxg:LookUpEdit Name="lookUpEdit1" ItemsSource="{Binding Path=Products}"
HorizontalAlignment="Center" VerticalAlignment="Center"
Width="200"
DisplayMember="ProductName"
ValueMember="ID"
AddNewButtonPlacement="EditBox"
AutoComplete="True"
IncrementalFiltering="True"
AutoPopulateColumns="True"
ImmediatePopup="True"
IsPopupAutoWidth="False">
<dxmvvm:Interaction.Triggers>
<dxmvvm:EventToCommand PassEventArgsToCommand="True" Command="{Binding ShowProductFormCommand}" EventName="ProcessNewValue" SourceName="lookUpEdit1">
<dxmvvm:EventToCommand.EventArgsConverter>
<viewmodel:ProductListValueConverter/>
</dxmvvm:EventToCommand.EventArgsConverter>
</dxmvvm:EventToCommand>
</dxmvvm:Interaction.Triggers>
</dxg:LookUpEdit>
</Grid>
<dxmvvm:Interaction.Behaviors>
<dx:DialogService DialogWindowStartupLocation="CenterOwner">
<dx:DialogService.ViewTemplate>
<DataTemplate>
<local:UserControl1/>
</DataTemplate>
</dx:DialogService.ViewTemplate>
<dx:DialogService.DialogStyle>
<Style TargetType="dx:DXDialogWindow">
<Setter Property="Width" Value="345"/>
<Setter Property="Height" Value="324"/>
</Style>
</dx:DialogService.DialogStyle>
</dx:DialogService>
</dxmvvm:Interaction.Behaviors>
</Window>
<UserControl x:Class="HowToCreateLookUpEdit.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HowToCreateLookUpEdit"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:viewmodel="clr-namespace:HowToCreateLookUpEdit.ViewModel"
mc:Ignorable="d" d:DesignWidth="322" Height="324">
<Grid>
<StackPanel Margin="8">
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal"
Margin="0,0,0,12">
<TextBlock VerticalAlignment="Center" Text="Product Name:" Margin="0,0,4,0"/>
<dxe:TextEdit VerticalAlignment="Center" IsReadOnly="True"
EditValue="{Binding Path=ProductName, Mode=OneWay}"/>
</StackPanel>
<GroupBox Header="New Value Properties" Grid.Row="1" Grid.Column="1"
VerticalAlignment="Top" MinWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Country:" VerticalAlignment="Center"
Grid.Row="0" Grid.Column="0" Margin="0,0,6,4" />
<dxe:TextEdit x:Name="txtCountry" Grid.Row="0" Grid.Column="1"
EditValue="{Binding Path=Country, Mode=TwoWay}" Margin="0,0,0,4"/>
<TextBlock Text="City:" VerticalAlignment="Center" Grid.Row="1"
Grid.Column="0" Margin="0,0,6,4" />
<dxe:TextEdit x:Name="txtCity" Grid.Row="1" Grid.Column="1"
EditValue="{Binding Path=City, Mode=TwoWay}" Margin="0,0,0,4"/>
<TextBlock Text="Quantity:" VerticalAlignment="Center" Grid.Row="2"
Grid.Column="0" Margin="0,0,6,4" />
<dxe:TextEdit x:Name="txtQuantity" Grid.Row="2" Grid.Column="1"
EditValue="{Binding Path=Quantity, Mode=TwoWay}" Margin="0,0,0,4"/>
<TextBlock Text="Unit Price:" VerticalAlignment="Center" Grid.Row="3"
Grid.Column="0" Margin="0,0,6,4" />
<dxe:SpinEdit x:Name="spUnitPrice" Grid.Row="3" Grid.Column="1"
EditValue="{Binding Path=UnitPrice, Mode=TwoWay, Converter={StaticResource converter}}" Margin="0,0,0,4"/>
<TextBlock Text="Product ID:" VerticalAlignment="Center" Grid.Row="4"
Grid.Column="0" Margin="0,0,6,4" />
<dxe:SpinEdit x:Name="spId" Grid.Row="4" Grid.Column="1"
EditValue="{Binding Path=ID, Mode=TwoWay, Converter={StaticResource converter}}" Margin="0,0,0,4"/>
</Grid>
</GroupBox>
</StackPanel>
</Grid>
</UserControl>
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Namespace HowToCreateLookUpEdit
''' <summary>
''' Interaction logic for UserControl1.xaml
''' </summary>
Partial Public Class UserControl1
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
End Class
End Namespace
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Windows
Namespace HowToCreateLookUpEdit
''' <summary>
''' Interaction logic for App.xaml
''' </summary>
Partial Public Class App
Inherits Application
End Class
End Namespace
using System.Collections.Generic;
using System.Collections.ObjectModel;
using DevExpress.Mvvm;
namespace HowToCreateLookUpEdit.ViewModel
{
public class Product
{
public int ID { get; set; }
public string ProductName { get; set; }
public string Country { get; set; }
public string City { get; set; }
public int UnitPrice { get; set; }
public int Quantity { get; set; }
}
}
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;
namespace HowToCreateLookUpEdit
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Editors;
using DevExpress.Mvvm;
using DevExpress.Mvvm.UI;
namespace HowToCreateLookUpEdit.ViewModel {
public class ProductListViewModel : ViewModelBase {
ObservableCollection<Product> products = new ObservableCollection<Product>();
public ObservableCollection<Product> Products { get { return products; } }
public ProductListViewModel()
: base() {
products.Add(new Product() { ProductName = "Chang", Country = "UK", City = "Cowes", UnitPrice = 19, Quantity = 10, ID = 1000 });
products.Add(new Product() { ProductName = "Gravad lax", Country = "Italy", City = "Reggio Emilia", UnitPrice = 12, Quantity = 16, ID = 1001 });
products.Add(new Product() { ProductName = "Ravioli Angelo", Country = "Brazil", City = "Rio de Janeiro", UnitPrice = 19, Quantity = 12, ID = 1002 });
products.Add(new Product() { ProductName = "Tarte au sucre", Country = "Germany", City = "QUICK-Stop", UnitPrice = 22, Quantity = 50, ID = 1003 });
products.Add(new Product() { ProductName = "Steeleye Stout", Country = "USA", City = "Reggio Emilia", UnitPrice = 18, Quantity = 20, ID = 1004 });
products.Add(new Product() { ProductName = "Pavlova", Country = "Austria", City = "Graz", UnitPrice = 21, Quantity = 52, ID = 1005 });
products.Add(new Product() { ProductName = "Longlife Tofu", Country = "USA", City = "Boise", UnitPrice = 7, Quantity = 120, ID = 1006 });
products.Add(new Product() { ProductName = "Alice Mutton", Country = "Mexico", City = "México D.F.", UnitPrice = 21, Quantity = 15, ID = 1007 });
products.Add(new Product() { ProductName = "Alice Mutton", Country = "Canada", City = "Tsawwassen", UnitPrice = 44, Quantity = 16, ID = 1008 });
}
public ICommand _ShowProductFormCommand;
public ICommand ShowProductFormCommand {
get {
if (_ShowProductFormCommand == null)
_ShowProductFormCommand = new DelegateCommand<string>(OnShowProductFormCommandExecute);
return _ShowProductFormCommand;
}
}
IDialogService DialogService { get { return GetService<IDialogService>(); } }
private void OnShowProductFormCommandExecute(string parameter) {
if (parameter == string.Empty) return;
Product p = new Product() { ProductName = parameter };
UICommand addProductCommand = new UICommand() {
Caption = "Add",
IsCancel = false,
IsDefault = true
};
UICommand cancelProductCommand = new UICommand() {
Caption = "Cancel",
IsDefault = false,
IsCancel = true
};
UICommand result = DialogService.ShowDialog(
dialogCommands: new List<UICommand>() { addProductCommand, cancelProductCommand },
title: "Add new row",
viewModel: p);
if (result == addProductCommand) {
products.Add(p);
}
}
}
public class ProductListValueConverter : IEventArgsConverter {
public object Convert(object sender, object args) {
string productName = ((ProcessNewValueEventArgs)args).DisplayText;
return productName;
}
}
public class StringToIntConverter : IValueConverter {
#region IValueConverter Members
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return (int)value;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return value.ToString();
}
#endregion
}
}
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Grid.LookUp;
using HowToCreateLookUpEdit.ViewModel;
namespace HowToCreateLookUpEdit {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow()
{
InitializeComponent();
}
}
}