Friday, March 13, 2009

Silverlight2: ComboBox in DataGrid

Friday, March 13, 2009 Posted by Andre Broers 4 comments
In this sample a will show a combobox item in a datagrid. It is best to update the datagrid component to the latest:



I will create a Lookup table which will only be loaded once for the whole datagrid which is good for performance.

Start with a ProductProvider:




namespace SilverlightApplication2
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int ProductType { get; set; }
}
}




using System.Collections.ObjectModel;

namespace SilverlightApplication2
{
public class ProductProvider
{
public Collection Products
{
get
{
Collection
ReturnProducts = new Collection();
ReturnProducts.Add(new Product() { ProductId = 1, ProductName = "Product1", ProductType = 1 });
ReturnProducts.Add(new Product() { ProductId = 2, ProductName = "Product2", ProductType = 2 });
ReturnProducts.Add(new Product() { ProductId = 3, ProductName = "Product3", ProductType = 3 });
return ReturnProducts;
}
}
}
}


The lookupprovider:




namespace SilverlightApplication2
{
public class Lookup
{
public int LookupId { get; set; }
public string LookupString { get; set; }
}
}




using System.Collections.ObjectModel;

namespace SilverlightApplication2
{
public class LookupProvider
{
private Collection _Lookups;

public LookupProvider()
{
_Lookups = new Collection
();
_Lookups.Add(new Lookup() { LookupId = 1, LookupString = "Type1" });
_Lookups.Add(new Lookup() { LookupId = 2, LookupString = "Type2" });
_Lookups.Add(new Lookup() { LookupId = 3, LookupString = "Type3" });
}

public Collection
Lookups
{
get
{
return _Lookups;
}
}
}

}


ComboBoxText Usercontrol:



<UserControl x:Class="SilverlightApplication2.ComboBoxText"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Grid x:Name="LayoutRoot">
<TextBlock x:Name="Text">TextBlock>
Grid>
UserControl>




using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace SilverlightApplication2
{
public partial class ComboBoxText : UserControl
{
public int Id
{
get { return (int)GetValue(IdProperty); }
set { SetValue(IdProperty, value); }
}

public Collection Lookups
{
get { return (Collection
)GetValue(LookupsProperty); }
set { SetValue(LookupsProperty, value); }
}

public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(int), typeof(ComboBoxText), new PropertyMetadata(new PropertyChangedCallback(ListBoxTextChangedCallback)));

public static readonly DependencyProperty LookupsProperty = DependencyProperty.Register("Lookups", typeof(Collection
), typeof(ComboBoxText), null);

private static void ListBoxTextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ComboBoxText r = d as ComboBoxText;

foreach (Lookup Lookup in r.Lookups)
{
if (Lookup.LookupId == (int)e.NewValue) {
r.Text.Text = Lookup.LookupString;
}
}
}

public ComboBoxText()
{
InitializeComponent();
}
}
}


ComboBox Usercontrol:



<UserControl x:Class="SilverlightApplication2.ComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:SilverlightApplication2" >
<Grid x:Name="LayoutRoot">
<ComboBox x:Name="Combo" SelectionChanged="ComboBox_SelectionChanged">ComboBox>
Grid>
UserControl>




using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace SilverlightApplication2
{
public partial class ComboBox : UserControl
{
public int Id
{
get { return (int)GetValue(IdProperty); }
set { SetValue(IdProperty, value); }
}

public Collection Lookups
{
get { return (Collection
)GetValue(LookupsProperty); }
set { SetValue(LookupsProperty, value); }
}

public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(int), typeof(ComboBox), new PropertyMetadata(new PropertyChangedCallback(ComboBoxIdChangedCallback)));

public static readonly DependencyProperty LookupsProperty = DependencyProperty.Register("Lookups", typeof(Collection
), typeof(ComboBox), new PropertyMetadata(new PropertyChangedCallback(ComboBoxLookupsChangedCallback)));

private static void ComboBoxIdChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ComboBox r = d as ComboBox;

foreach (Lookup Lookup in r.Lookups)
{
if (Lookup.LookupId == (int)e.NewValue) r.Combo.SelectedItem = Lookup;
}
}

private static void ComboBoxLookupsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ComboBox r = d as ComboBox;

r.Combo.ItemsSource = r.Lookups;
r.Combo.DisplayMemberPath = "LookupString";
}

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (Lookup Lookup in e.AddedItems)
{
Id = Lookup.LookupId;
}
}

public ComboBox()
{
InitializeComponent();
}
}
}


And the page to make it all happen:



<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightApplication2.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:my="clr-namespace:SilverlightApplication2">
<UserControl.Resources>
<my:ProductProvider x:Key="productProvider"/>
<my:LookupProvider x:Key="lookupProvider"/>
UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<data:DataGrid x:Name="TheGrid" AutoGenerateColumns="False" ItemsSource="{Binding Products, Source={StaticResource productProvider}}" >
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="ProductId" Binding="{Binding ProductId}" />
<data:DataGridTextColumn Header="ProductName" Binding="{Binding ProductName}" />
<data:DataGridTemplateColumn Header="Type">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<my:ComboBoxText Id="{Binding ProductType}" Lookups="{Binding Lookups, Source={StaticResource lookupProvider}}" Margin="4" />
DataTemplate>
data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<my:ComboBox Lookups="{Binding Lookups, Source={StaticResource lookupProvider}}" Id="{Binding ProductType, Mode=TwoWay}" >my:ComboBox>
DataTemplate>
data:DataGridTemplateColumn.CellEditingTemplate>
data:DataGridTemplateColumn>
data:DataGrid.Columns>
data:DataGrid>
StackPanel>
Grid>
UserControl>

4 comments:

  1. Quester is a Pakistan-based questioning answering website where people can ask questions and we try our best to provide them with the best answers. Anyone can ask any legit question in English or Roman Urdu and we provide answers in the same language format. www.quester.pk

    ReplyDelete
  2. Coach Outlet http://www.coachoutletfactory-stores.us.com

    Kate Spade Outlet http://www.katespadefactoryoutlet.in.net

    ED Hardy Outlet http://www.edhardypopsale.com

    Coach Outlet Store Online http://www.coachstoreonline.in.net

    Kate Spade Outlet http://www.katespadeoutletstore.in.net

    Cheap Jordans http://www.cheapjordansretro.us.com

    Coach Purses http://www.coachpurses.eu.com

    Coach Outlet http://www.coachoutlet2016.us.com

    Tory Burch Shoes http://www.toryburchoutlets.us.com

    Longchamp Outlet http://www.longchampoutlet.in.net

    Michael Kors Handbags http://www.innnov8.com

    Coach Outlet http://www.innnovate.com

    Red Bottom Shoes http://www.redbottomshoes.us.org

    Moncler Jackets http://www.monclerdownjackets.us.com

    Kate Spade Outlet http://www.katespadeoutletstore.us.com

    Moncler Outlet http://www.moncleroutletonline.in.net

    Coach Factory http://www.maganates.com

    Coach Factory Outlet http://www.coachfactorysoutlet.in.net

    Kate Spade Outlet http://www.katespadesoutlet.us.com

    Ray Ban Outlet http://www.raybanoutletstore.in.net

    Christian Louboutin Shoes http://www.christianlouboutinshoes2016.in.net

    North Face Outlet http://www.northfaceoutlets.us.org

    Coach Factory Outlet http://www.coachoutletstore.name

    Kate Spade Outlet http://www.katespadeoutletonline.name

    Coach Outlet http://www.coach-outlets.us.org

    Nike Roshe Run http://www.tanie-nikerosherun.pl

    Buty Nike Air Max http://www.airmaxsprzedazdla.pl

    Nike Air Max http://www.nikeairmax.us.org

    Red Bottom Shoes http://www.redbottomshoe.in.net

    Christian Louboutin Outlet http://www.christianlouboutinoutlet.us.org

    Balenciaga Outlet http://www.balenciaga-outlet.us.com

    Balenciaga Handbags http://www.balenciaga-handbags.us.com

    Kate Spade Outlet http://www.katespadefactoryoutlet.in.net

    Toms Shoes http://www.tomshoesoutlet.us.com

    Hermes Belt http://www.hermes-belts.us.com

    Prada Outlet http://www.pradafactoryoutlet.com

    Louis Vuitton Outlet http://www.louisvuittonsoutlet.us.com

    North Face Outlet Store http://www.northfaceoutletstore.in.net

    North Face Jackets http://www.north-face.in.net

    North Face http://www.north-faceoutlets.us.com

    Louis Vuitton Outlet http://www.louisvuitton-outlets.us.org

    Timberland Boots http://www.timberland-outlets.us.com

    Cheap Timberland Boots http://www.timberlandoutlet.us.com

    Tory Burch Outlet http://www.toryburch-outlets.us.com

    Ralph Lauren Outlet http://www.ralphlaurenstoreoutlet.us.com

    Gucci Shoes http://www.guccifactoryoutlet.name

    Gucci Outlet http://www.guccioutletinc.us.com

    North Face Jackets http://www.northfacefactoryoutlet.us.com

    Prada Outlet http://www.pradaoutlets.in.net

    Hollister Clothing http://www.hollister-clothing.us.com

    Ferragamo Shoes http://www.ferragamoshoes.us.org

    Tiffany Outlet http://www.tiffany-outlets.us.com

    Tiffany Outlet http://www.tiffanyco-outlet.us.com

    NFL Jerseys http://www.nfljerseys.name

    Toms Outlet http://www.outlettoms.us.com

    Cheap Jordans http://www.cheapjordans.eu.com

    North Face Outlet http://www.thenorthfaceoutlet.us.org

    Prada Handbags http://www.pradaoutlet.us.org

    Chan Luu http://www.chanluu.us.com

    Toms Outlet http://www.tomsoutletstores.us.com

    Kate Spade Outlet http://www.katespadeoutletofficial.us.com

    Beats By Dr Dre http://www.beatsbydrdre.us.org

    Coach Outlet http://www.coachstoreoutlet.us.org

    Christian Louboutin Shoes http://www.christianlouboutinshoe.eu.com

    Valentino Shoes http://www.valentinoshoes.eu.com

    Michael Kors Outlet http://www.michaelkorsoutlet.us.org

    Coach Factory Outlet http://www.coachfactoryoutlet.us.org

    Coach Outlet Online http://www.coachoutletonlinestore.us.org

    Coach Purses http://www.coachpurse.us.com

    ReplyDelete