Search code examples
c#silverlight

Populate ComboBox from code behind to display in MyComboBox


I have following in Silverlight xaml:

<UserControl........
<ScrollViewer Grid.Row="2">
        <ListBox x:Name="lbList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <Image Source="images/Bild1.png" Grid.Column="0"/>
                        <ComboBox Name="MyComboBox" Grid.Column="1"></ComboBox>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>
    .............
 </UserControl>

And in code behind:

  static Dictionary<string, string> dictDocTypes

which I populate with some Data. How to show that Data inside the MyComboBox?


Solution

  • Way 1 Put loaded event on your combo. In that typecast sender to ComboBox and populate your data in it.

    Way 2 (Pure MVVM)
    Attach command to event. Read here. Pass self (Combo) in it. In VM command, typecast parameter object to Combo and populate it.