Skip to main content

Microsoft Silverlight

Unanswered Question Combobox in datagrid: Set the ItemSource at Run timeRSS Feed

(0)

omkar_y
omkar_y

Member

Member

4 points

8 Posts

Combobox in datagrid: Set the ItemSource at Run time

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
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: Combobox in datagrid: Set the ItemSource at Run time

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?

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

omkar_y
omkar_y

Member

Member

4 points

8 Posts

Re: Combobox in datagrid: Set the ItemSource at Run time

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.

omkar_y
omkar_y

Member

Member

4 points

8 Posts

Re: Combobox in datagrid: Set the ItemSource at Run time

 

                        <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.Barskiy
Sergey.B...

Member

Member

40 points

31 Posts

Re: Combobox in datagrid: Set the ItemSource at Run time

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?

Sergey Barskiy.

omkar_y
omkar_y

Member

Member

4 points

8 Posts

Re: Combobox in datagrid: Set the ItemSource at Run time

I intially set the MaterialList property to return from something like

public List<Material> MaterialList

{

 get

 {

return new List<Material> {

new Material() { MatDesc = "Mat1", MaterialId = 1},

 new Material() { MatDesc = Mat2 ",  MaterialId = 2},

 new Material() { MatDesc = "3", salMaterialId = 3}

 };

 }

 }

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.

 

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: Re: Combobox in datagrid: Set the ItemSource at Run time

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.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

omkar_y
omkar_y

Member

Member

4 points

8 Posts

Re: Re: Combobox in datagrid: Set the ItemSource at Run time

Bryant,

But how do I bind the objMaterialListProvider.MaterialList to the ComboBox of the  DataTemplate. That is where I am struck right now.

 

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: Re: Re: Combobox in datagrid: Set the ItemSource at Run time

It looks like your binding is setup correctly. Is it not working?

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

omkar_y
omkar_y

Member

Member

4 points

8 Posts

Re: Re: Re: Combobox in datagrid: Set the ItemSource at Run time

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 Tang - MSFT
Min-Hong...

Contributor

Contributor

3619 points

412 Posts

Re: Combobox in datagrid: Set the ItemSource at Run time

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

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities