WPF Toolkit - Datagrid - ComboboxColumn Binding w/DynamicResource

Jul 22, 2009 at 1:37 PM

I am implementing the WPF DataGrid (very new to WPF). I followed tutorials that showed how to bind the comboboxcolumn using staticresources. However, the databinding for a few columns in my datagrid will not be known until runtime.

Because of this, I can't bind them with the staticresource. Is there any other way to databind the comboboxcolumns in a datagrid? In ASP.NET, I know we had the rowdatabound code where we could do this and dynamically create the contents of the columns. But, in WPF, it looks like everything is done through resources.

How can you databind using dynamic resources in the datagrid?

Thanks!

Jul 22, 2009 at 2:27 PM

You can use the ComboBox.ItemsSource and set it to an ObservableCollection or an equivalent. The class that is part of the collection should override the ToString method and return the text you want displayed in the ComboBox text box. All the columns you want to bind to in the class must be properties. If you want I could provide some C# sample code.

 

Tom Groszko

Jul 22, 2009 at 2:31 PM

The C# sample code would be very helpful. I'm binding like: <toolkit:DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource OBJ_DATA_PROVIDER_NAME}}"... I've only seen samples that show how to bind to an object data provider in the XAML using a resource, but not directly binding to the object data provider that is not a static resource.

Jul 22, 2009 at 8:42 PM

<Window x:Class="ComboBoxExample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="200" Width="600">
    <Grid>
 <ComboBox Name="ASampleComboBox" Height="23" Width="300" Margin="44,86,114,0" VerticalAlignment="Top" SelectionChanged="ASampleComboBox_SelectionChanged" />
    </Grid>
</Window>

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;
using System.Collections.ObjectModel; // <=== this is where ObservableCollection lives

namespace ComboBoxExample
{ public partial class Window1: Window
 { public PersonObservableCollection  MyPersonCollection = null;
  public Window1 ()
  { InitializeComponent ();
   MyPersonCollection = new PersonObservableCollection();
   MyPersonCollection.Add(new PersonName("Jones", "James", "", "The Big Guy"));
   MyPersonCollection.Add(new PersonName("Smith", "John", "James", "JJ"));
   MyPersonCollection.Add(new PersonName("Potter", "Harry", "", "The Wize Guy"));
   ASampleComboBox.ItemsSource = MyPersonCollection;
  }
  private void ASampleComboBox_SelectionChanged (object sender, SelectionChangedEventArgs e)
  { if (ASampleComboBox.SelectedItem != null)
   { PersonName SelectedPerson = (PersonName) ASampleComboBox.SelectedItem;
    Console.WriteLine(SelectedPerson.ToString());
   }
  }
 }
 public class PersonName
 { public String LastName  {get {return _LastName;}  set {_LastName  = value;}} private String _LastName  = String.Empty;
  public String FirstName  {get {return _FirstName;}  set {_FirstName  = value;}} private String _FirstName  = String.Empty;
  public String MiddleName {get {return _MiddleName;}  set {_MiddleName = value;}} private String _MiddleName  = String.Empty;
  public String PreferredName {get {return _PreferredName;} set {_PreferredName = value;}} private String _PreferredName = String.Empty;
  public String FullName  {get {return LastName + ", " + FirstName + " " + MiddleName + " (" + PreferredName + ")";}}
   public PersonName (String LastName, String FirstName, String MiddleName, String PreferredName)
   { this.LastName  = LastName;  
    this.FirstName  = FirstName;  
    this.MiddleName  = MiddleName; 
    this.PreferredName = PreferredName;
   }
  public override string ToString ()
  { return FullName;
  }
 }
 public class PersonObservableCollection  : ObservableCollection<PersonName>
 { public PersonObservableCollection() {}
 }
}