0
votes

I've got this XAML code:

<Window x:Class="New_app_2.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="400" Width="500">
    <Grid>
        <StackPanel Orientation="Horizontal">
            <ItemsControl ItemsSource="{Binding TestList}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding }"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <ComboBox ItemsSource="{Binding Tags}" VerticalAlignment="Top">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding}"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </StackPanel>
    </Grid>
</Window>

I bound TextBlock to a property TestList:

private List<string> _testList;

public List<string> TestList
{
    get
    {
        return new List<string>() { "Test1", "Test2", "Test3", "Test4", "Test5" };
    }
    set
    {
        _testList = value;
    }
}

and a ComboBox to a property Tags (which looks pretty much the same as TestList property, but have different strings).

My goal is to display that in a form like this:

Test1 [Combobox of tags]

Test2 [Combobox of tags]

Test3 [Combobox of tags]

Test4 [Combobox of tags]

Test5 [Combobox of tags]

Instead, I'm getting:

Test1 [Combobox of tags]

Test2

Test3

Test4

Test5

Later, I'd also like to be able to get information which options were chosen for each of TestList elements.

Probably it's very easy, but I've just started learning WPF.

1

1 Answers

1
votes

Shouldn't you just move the ComboBox into the ItemsControl.ItemTemplate then? (You will then need to wrap both TextBlock and the ComboBox in another panel (e.g. a StackPanel).)

If all ComboBoxes should contain the same items you will need to change the binding to access the outside DataContext.

i.e.

<ComboBox ItemsSource="{Binding DataContext.Tags,
                                RelativeSource={RelativeSource AncestorType=ItemsControl}}">