Datagrid on UserControl

Nov 24, 2008 at 10:10 PM
Is there a known issues with DataGrid palced on the WPF UserControl as opposed to the WPF window? I'm having an issue with the combobox. Here is the XAML:


dg:DataGridComboBoxColumn Header="Type" Width="60" x:Name="cbCustType" CellStyle="{StaticResource SingleClickEditing}" SelectedItemBinding="{Binding Path=CustType}" SelectedValuePath="CustType" DisplayMemberPath="Type Of Customer" SortMemberPath="CustType">
<Style TargetType="ComboBox">
<Setter Property="IsEditable" Value="True" />

Code behind
dgCustomers.ItemsSource = _customerList;



Firsts of all, combobox is not populated at all (although, when changed to Textcolumn is fine). And when clicked gives the following error: "Unable to cast object of type 'System.Windows.Controls.Primitives.ToggleButton' to type 'System.Windows.Controls.Button'".

Please, advise!

Nov 25, 2008 at 8:39 AM

Your ComboBox is bound to an array of strings. In this case you should not be specifying the SelectedValuePath, DisplayMemberPath or SortMemberPath - these are used when you bind to a collection of objects allowing you to specify which property of the bound object is used for the value, sorting etc...

See the following examples:

Colin E.
Nov 25, 2008 at 11:53 PM
Thanks anyway - it helped. I'm new to WPF - this was just one of my tests. Once I removed these, the default value from the business object showed up, although it disappears when I focus on it. But my bigest issue is to figure out how to link a collection to the comboox at runtime, preferably in the code - I don't have the collection at design time. Some example would be greatly appreciated!

Nov 26, 2008 at 8:03 AM

Here is a little example:

<dg:DataGrid Name="dataGrid" Loaded="dataGrid_Loaded" ItemsSource="{Binding}" AutoGenerateColumns="False" SelectionMode="Extended">
        <dg:DataGridComboBoxColumn x:Name="MyComboColumn" Header="Type" Width="60" SelectedItemBinding="{Binding Path=Name}"/>                    

The ComboBox column is given a x:Name, this enables us to locate it within the visual tree. We handle the Loaded event for the DataGrid, this occurs when the visual tree of the DataGrid is initially constructed.

Within the event handler we can locate our column then set the ItemsSource programmatically:

private void dataGrid_Loaded(object sender, RoutedEventArgs e)
    DataGrid grid = sender as DataGrid;
    DataGridComboBoxColumn comboBoxColumn = grid.FindName("MyComboColumn") as DataGridComboBoxColumn;
    comboBoxColumn.ItemsSource = new List<string>() { "one", "two", "three" };

Hope that helps,
Colin E.