WPF DataGrid Binding DataGridComboBoxColumn

WPF DataGrid Binding DataGridComboBoxColumn

Old forum URL: forums.lhotka.net/forums/t/6981.aspx


MikeB posted on Wednesday, May 20, 2009

I have a combo box with DepartmentList, each DepartmentBO object contains a list of activities. In the datagrid I have a DataGridComboBoxColumn that I need to be populated with the activities for the selected department. Any tips on how this can be accomplished?

Mike B

MikeB replied on Thursday, May 21, 2009

Alright, so trying to use the PTracker and the c#2008 book as a guide I have come up with the code below. The list of activities shows up in the DataGridCombo but this lists all activities. I need to show only those activitites contained in the currently selected department. Also, I don't know what to put in for Value or SelectedItem binding.  Any help would be appreciated.

<UserControl.Resources>

<csla:CslaDataProvider x:Key="ActivityList"

ObjectType="{x:Type Inform:TimesheetActivityList}"

IsAsynchronous="True"

FactoryMethod="GetAllActivities"

ManageObjectLifetime="True">

</csla:CslaDataProvider>

<csla:ObjectStatus x:Key="ActivityListStatus"

DataContext="{StaticResource ActivityList}"/>

</UserControl.Resources>

Here is the DataGridComboBoxColumn definition:

<dg:DataGridComboBoxColumn

x:Name="ActivitiesCombo"

Header="Activity"

Width="*"

ItemsSource="{Binding Source= {StaticResource ActivityList}}"

SelectedItemBinding="{Binding Path=TimesheetActivity}"

DisplayMemberPath="Name"/>

Don't know if this is the correct way to do this, but in WF the workaround is to catch the EditControlShowing event and set the items source there. I did this using the PrepareCellForEdit event.

private void PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)

{

if (e.Column is DataGridComboBoxColumn)

{

this.ActivitiesCombo.ItemsSource =

(this.cbDepartment.SelectedItem as DepartmentBO).TimesheetActivities;

}

}

My only outstanding issue is that the current value is not displayed in the cells...... How do I bind the value to the cells? As you can see from the xaml above, I do set the path for the SelectedItemBinding.... Any help is appreciated!

Mike B

Fintanv replied on Thursday, May 21, 2009

I use the Infragistics controls so my code is a little different, but it may point you in the right direction.  The Field is the infragistics equiv of a column.  The dataitem is the actual business object that is bound to the row.  The business object contains a list of the available product families which is used to populate the combo for the field.

<igDP:Field Name="ProductFamilyName" Label="{x:Static local:ModuleStrings.ProductFamilyName}">

<igDP:Field.Settings>

<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}">

<igDP:FieldSettings.EditorStyle>

<Style TargetType="{x:Type igEditors:XamComboEditor}">

<Setter Property="ItemsSource" Value="{Binding Path=DataItem.ProductFamilies}" />

<Setter Property="DisplayMemberPath" Value="ProductFamilyName" />

<Setter Property="ValuePath" Value="ProductFamilyName" />

</Style>

</igDP:FieldSettings.EditorStyle>

</igDP:FieldSettings>

</igDP:Field.Settings>

</igDP:Field>

Copyright (c) Marimer LLC