Home Forums WinForms controls Xceed Grid for WinForms Dynamically populated ComboBox Reply To: Dynamically populated ComboBox

User (Old forums)
Member
Post count: 23064

there are many ways to do this.
I have found that the simplest way is to populate the items in the combo box when the combo box has focus;
xaml example:
<xcdg:CellEditor x:Key=”GradeCellEditor”>
<xcdg:CellEditor.EditTemplate>
<DataTemplate>
<ComboBox BorderThickness=”0″
BorderBrush=”Transparent”
VerticalContentAlignment=”Top”
SelectedValuePath=”Grade”
DisplayMemberPath=”Grade”
IsEditable=”True”
Text=”{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType=xcdg:Cell},
Path=ActualContent,
UpdateSourceTrigger=PropertyChanged}”
GotFocus=”GradeComboBox_GotFocus”
Tag=”{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType=xcdg:Cell},
UpdateSourceTrigger=PropertyChanged}”>
</ComboBox>
</DataTemplate>
</xcdg:CellEditor.EditTemplate>
<xcdg:CellEditor.ActivationGestures>
<xcdg:TextInputActivationGesture/>
</xcdg:CellEditor.ActivationGestures>
</xcdg:CellEditor>

———————————————————————-
//…Code behind;
private void GradeComboBox_GotFocus(object sender, RoutedEventArgs e)
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
if (cb.ItemsSource == null)
{
DataCell ms = cb.Tag as DataCell;
if (ms != null)
{
ISize g = ms.DataContext as ISize;
if (g != null)
{
if (g.size.HasValue() && g.type.HasValue())
cb.ItemsSource = InitGrades(g.type, g.size);
else if (g.type.HasValue())
cb.ItemsSource = InitGrades(g.type);
}
else
{
IMType ty = ms.DataContext as IMType;
if (ty != null)
cb.ItemsSource = InitGrades(ty.type);
}
}
}
}
}

—————————————————————
Hope you find this example OK
I just copied and pasted my code so dont worry about the objects you dont know about.
the Tag filed is bound to the cell and the cell.datacontext is the rows datasource(your object)

I havenet seem much loss in speed vs. having the itemssource set in Xaml.
I have put the if(combobox.itemssource == null) in for speed reasons in not reloading the data
but every time a cell is edited the itemssource on the combobox is always null. To me this means the combobox is created each time you enter edit mode on that cell.

————————————————————-
As for unbound items in a combobox I have some tricks for that.
It has to do with the collection bound to the items source. not the combobox iteself.
look at this article I found to get more ideas
http://www.codeproject.com/cs/combobox/UnboundItemsComboBox.asp

Imported from legacy forums. Posted by MiddleTommy (had 520 views)