开发者

Populate ComboBox in Grid

Merged with Populate combobox with collection other than the one the Grid it is in is bound to.

I have a ViewModel with two collection properties. One collection I use to fill form fields on the page that has its DataContext set to this ViewModel. On this form, amongst the fields being populated with the collection, I also have a ComboBox that I want to fill with the other collection.

I use a Grid to do my layout, and this Grid's DataContext is set to开发者_运维百科 the first collection. When I have a ComboBox outside of the Grid I can easily fill it with the second collection. But once I try to do so when it is in the grid, I can't seem to get it done.

In the code snippet, I set the Grid's DataContext to ItemTypes. From ItemTypes the fields can get their values. The ComboBox though needs to get its values from the other collection:

<Grid DataContext="{Binding ItemTypes}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock Text="Name:" />
        <TextBox Grid.Column="2" Text="{Binding Name}" />

        <TextBlock Grid.Row="1" Text="Description:" />
        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Description}" />

        <TextBlock Grid.Row="2" Text="Manufacturer:" />
        <ComboBox Grid.Column="1" Grid.Row="2"  />

        <TextBlock Grid.Row="3" Text="Short Name:" />
        <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding ShortName}" />

        <TextBlock Grid.Row="5" Text="Weight:"/>
        <TextBox Grid.Column="1" Grid.Row="5" Text="{Binding Weight}" />
    </Grid>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜