Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Combobox in datagrid: Set the ItemSource at Run time
10 replies. Latest Post by Min-Hong Tang - MSFT on November 6, 2009.
(0)
omkar_y
Member
4 points
8 Posts
11-03-2009 5:13 PM |
I have a datagrid with one of the columns as DataTemplate (ComboBox). I have to set the ItemSource of Combobox at runtime.
I tried to follow the example of
http://weblogs.asp.net/manishdalal/archive/2008/09/28/combobox-in-datagrid.aspx
but here the list of the cities in static.
Could anybody let me know if they tried a similar scenario. Please let me know
bryant
Star
9937 points
1,629 Posts
11-03-2009 5:15 PM |
The answer will depend on what you're trying to set the itemsource to. Are you trying to bind it? Does it relate to the item?
11-03-2009 5:22 PM |
I have to bind it so that the user can change from the combo box. I have the relation established but not able to bind the combobox list at runtime. The default display item shows up fine.
11-03-2009 5:28 PM |
<my:DataGridTemplateColumn Header="Material " SortMemberPath="MaterialInfo.MatDesc"> <my:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding MaterialInfo.MatDesc}" /> </DataTemplate> </my:DataGridTemplateColumn.CellTemplate> <my:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding MaterialList, Source={StaticResource materialListProvider}}" SelectedItem="{Binding MaterialInfo, Mode=TwoWay}" DisplayMemberPath="MatDesc" src:ComboBoxService.ForceOpen="true" /> </DataTemplate> </my:DataGridTemplateColumn.CellEditingTemplate> </my:DataGridTemplateColumn>
That is my DataGridTemplateColumn. The materialListProvider is defined as in the UserResources as
<UserControl.Resources> <src:MaterialListProvider x:Key="materialListProvider"/> </UserControl.Resources>
The MaterialListProvider is a class that I defined with MaterialList as a function that returns a list of Materials. The issue is that I have to generate MaterialList during runtime (so thinking of defining an instance of MaterialListProvider and setting up the values). But that is not binding to the combobox( the dropdown shows empty on edit) :(
Sergey.B...
40 points
31 Posts
11-03-2009 5:35 PM |
Can you confirm that property MaterialList is being accessed? Does this property make a call to a server somewhere? If so, what do you do when this call completes?
11-03-2009 6:10 PM |
I intially set the MaterialList property to return from something like
public List<Material> MaterialList
This one worked fine.
Now I have a to set the list from a call from database at runtime. I am thinking of making a instance of the MaterialProvider class and settign the value of the MaterialInfo from the list retunred from database but don't know how to set it to the combobox from the instance of the MaterialProvider.
11-03-2009 6:24 PM |
Make sure your MaterialListProvider class implement INotifyPropertyChanged and that once you get the list from the database you fire the PropertyChanged event and specify MaterialList as the property name.
11-03-2009 7:58 PM |
Bryant,
But how do I bind the objMaterialListProvider.MaterialList to the ComboBox of the DataTemplate. That is where I am struck right now.
11-04-2009 3:57 PM |
It looks like your binding is setup correctly. Is it not working?
11-05-2009 3:36 PM |
It was working for the predefined list of materials. My goal is to bind to a list which is dynamic. tried a couple of ways but no luck so far
Min-Hong...
Contributor
3619 points
412 Posts
11-06-2009 2:43 AM |
HI,
Are you trying to bind comboBox using codeBehind. If that is the case. You could try to add a loaded event handler to the combox. And in the handler assign the comboBox's itemsSource to what you want.
If that is not the case, you may provide more information to us so that we could get a clear view of your scenerio. Then we can find you a more satisfied solution.
Best Regards