Instance initializer provides the functionality to create objects of a class that does not contain the default constructor (without the parameters).
<Application x:Class="pgrid_collection.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:pgrid_collection"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" x:Class="pgrid_collection.MainWindow"
Title="MainWindow" Height="500" Width="1000">
<Window.Resources>
<ResourceDictionary>
<local:ItemInitializer x:Key="itemInitializer" />
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="400" />
</Grid.ColumnDefinitions>
<dxprg:PropertyGridControl Name="pGrid" SelectedObject="{Binding Path=CurrentItem, ElementName=grid}" Grid.Column="1" UseCollectionEditor="{x:Null}"
ShowProperties="WithPropertyDefinitions" ShowCategories="False" CellValueChanged="pGrid_CellValueChanged" >
<dxprg:PropertyDefinition Path="ProductName" />
<dxprg:PropertyDefinition Path="City" />
<dxprg:PropertyDefinition Path="Country" />
<dxprg:PropertyDefinition Path="UnitPrice" />
<dxprg:PropertyDefinition Path="Quantity" />
<dxprg:CollectionDefinition Path="Suppliers" Type="{x:Type local:SupplierBase}"
NewItemInitializer="{StaticResource itemInitializer}" UseCollectionEditor="True" >
<dxprg:PropertyDefinition Path="*.FirstName" />
<dxprg:PropertyDefinition Path="*.LastName" />
<dxprg:PropertyDefinition Path="*.Phone" />
</dxprg:CollectionDefinition>
</dxprg:PropertyGridControl>
<dxg:GridControl Name="grid" Grid.Column="0">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="ProductName" />
<dxg:GridColumn FieldName="Country" />
<dxg:GridColumn FieldName="City" />
<dxg:GridColumn FieldName="UnitPrice" />
<dxg:GridColumn FieldName="Quantity" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView AllowEditing="False" />
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows
Namespace pgrid_collection
''' <summary>
''' Interaction logic for App.xaml
''' </summary>
Partial Public Class App
Inherits Application
End Class
End Namespace
Imports System.Collections.Generic
Namespace pgrid_collection
Public Class Supplier
Public Property FirstName() As String
Public Property LastName() As String
Public Property Phone() As String
End Class
Public Class SupplierBase
Inherits List(Of Supplier)
Public Sub New()
MyBase.New()
End Sub
End Class
Public Class Product
Public Property ProductName() As String
Public Property Country() As String
Public Property City() As String
Public Property UnitPrice() As Double
Public Property Quantity() As Integer
Public Property Suppliers() As SupplierBase
End Class
Public Class ProductList
Inherits List(Of Product)
Public Sub New()
MyBase.New()
Add(New Product() With { _
.ProductName = "Chang", _
.Country = "UK", _
.City = "Cowes", _
.UnitPrice = 19, _
.Quantity = 10, _
.Suppliers = New SupplierBase() _
})
Add(New Product() With { _
.ProductName = "Gravad lax", _
.Country = "Italy", _
.City = "Reggio Emilia", _
.UnitPrice = 12.5, _
.Quantity = 16, _
.Suppliers = New SupplierBase() _
})
Add(New Product() With { _
.ProductName = "Ravioli Angelo", _
.Country = "Brazil", _
.City = "Rio de Janeiro", _
.UnitPrice = 19, _
.Quantity = 12, _
.Suppliers = New SupplierBase() _
})
Add(New Product() With { _
.ProductName = "Tarte au sucre", _
.Country = "Germany", _
.City = "QUICK-Stop", _
.UnitPrice = 22, _
.Quantity = 50, _
.Suppliers = New SupplierBase() _
})
Add(New Product() With { _
.ProductName = "Steeleye Stout", _
.Country = "USA", _
.City = "Reggio Emilia", _
.UnitPrice = 18, _
.Quantity = 20, _
.Suppliers = New SupplierBase() _
})
Add(New Product() With { _
.ProductName = "Pavlova", _
.Country = "Austria", _
.City = "Graz", _
.UnitPrice = 21, _
.Quantity = 52, _
.Suppliers = New SupplierBase() _
})
Add(New Product() With { _
.ProductName = "Longlife Tofu", _
.Country = "USA", _
.City = "Boise", _
.UnitPrice = 7.75, _
.Quantity = 120, _
.Suppliers = New SupplierBase() _
})
Add(New Product() With { _
.ProductName = "Alice Mutton", _
.Country = "Canada", _
.City = "Tsawwassen", _
.UnitPrice = 44, _
.Quantity = 16, _
.Suppliers = New SupplierBase() _
})
End Sub
End Class
End Namespace
Imports System.Collections.Generic
Imports System.Windows
Imports DevExpress.Mvvm.Native
Imports DevExpress.Xpf.PropertyGrid
Namespace pgrid_collection
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
grid.ItemsSource = New ProductList()
End Sub
Private Sub pGrid_CellValueChanged(ByVal sender As Object, ByVal args As DevExpress.Xpf.PropertyGrid.CellValueChangedEventArgs)
Dim rowhandle = grid.GetSelectedRowHandles()(0)
grid.RefreshRow(rowhandle)
End Sub
End Class
Public Class ItemInitializer
Implements IInstanceInitializer
Public Sub New()
End Sub
Private Function IInstanceInitializer_CreateInstance(ByVal type As TypeInfo) As Object Implements IInstanceInitializer.CreateInstance
Dim item = New Supplier()
item.FirstName = "FIRSTNAME"
item.LastName = "LASTNAME"
item.Phone = "PHONE"
Return item
End Function
Private ReadOnly Property IInstanceInitializer_Types() As IEnumerable(Of TypeInfo) Implements IInstanceInitializer.Types
Get
Return New List(Of TypeInfo)() From {New TypeInfo(GetType(Supplier), "New Supplier")}
End Get
End Property
End Class
End Namespace
using System.Collections.Generic;
using System.Windows;
using DevExpress.Mvvm.Native;
using DevExpress.Xpf.PropertyGrid;
namespace pgrid_collection {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
grid.ItemsSource = new ProductList();
}
private void pGrid_CellValueChanged(object sender, DevExpress.Xpf.PropertyGrid.CellValueChangedEventArgs args) {
var rowhandle = grid.GetSelectedRowHandles()[0];
grid.RefreshRow(rowhandle);
}
}
public class ItemInitializer : IInstanceInitializer {
public ItemInitializer() {
}
object IInstanceInitializer.CreateInstance(TypeInfo type) {
var item = new Supplier();
item.FirstName = "FIRSTNAME";
item.LastName = "LASTNAME";
item.Phone = "PHONE";
return item;
}
IEnumerable<TypeInfo> IInstanceInitializer.Types {
get {
return new List<TypeInfo>() {
new TypeInfo(typeof(Supplier), "New Supplier"),
};
}
}
}
}
using System.Collections.Generic;
namespace pgrid_collection {
public class Supplier {
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
}
public class SupplierBase : List<Supplier> {
public SupplierBase()
: base() {
}
}
public class Product {
public string ProductName { get; set; }
public string Country { get; set; }
public string City { get; set; }
public double UnitPrice { get; set; }
public int Quantity { get; set; }
public SupplierBase Suppliers { get; set; }
}
public class ProductList : List<Product> {
public ProductList()
: base() {
Add(new Product() { ProductName = "Chang", Country = "UK", City = "Cowes", UnitPrice = 19, Quantity = 10, Suppliers = new SupplierBase() });
Add(new Product() { ProductName = "Gravad lax", Country = "Italy", City = "Reggio Emilia", UnitPrice = 12.5, Quantity = 16, Suppliers = new SupplierBase() });
Add(new Product() { ProductName = "Ravioli Angelo", Country = "Brazil", City = "Rio de Janeiro", UnitPrice = 19, Quantity = 12, Suppliers = new SupplierBase() });
Add(new Product() { ProductName = "Tarte au sucre", Country = "Germany", City = "QUICK-Stop", UnitPrice = 22, Quantity = 50, Suppliers = new SupplierBase() });
Add(new Product() { ProductName = "Steeleye Stout", Country = "USA", City = "Reggio Emilia", UnitPrice = 18, Quantity = 20, Suppliers = new SupplierBase() });
Add(new Product() { ProductName = "Pavlova", Country = "Austria", City = "Graz", UnitPrice = 21, Quantity = 52, Suppliers = new SupplierBase() });
Add(new Product() { ProductName = "Longlife Tofu", Country = "USA", City = "Boise", UnitPrice = 7.75, Quantity = 120, Suppliers = new SupplierBase() });
Add(new Product() { ProductName = "Alice Mutton", Country = "Canada", City = "Tsawwassen", UnitPrice = 44, Quantity = 16, Suppliers = new SupplierBase() });
}
}
}