开发者

WPF ComboBox text inside ListBox

I've got a list of ComboBoxes inside a listbox, like so:

    <ListBox ItemsSource="{Bind开发者_StackOverflowing Values}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ComboBox ItemsSource="{...}" IsTextSearchEnabled="True" IsEditable="True"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

The Values for my listbox ItemsSouce is defined in my ViewModel as

    public ObservableCollection<string> Values { get; set; }

How can I get the Text for each one of the ComboBoxes to show the Value for that particular ListBox item?

(i.e. if values is {"a", "b", "c"} I want a list of 3 comboboxes showing "a", "b" and "c")


Try this.

<ListBox ItemsSource="{Binding Values}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ComboBox Text="{Binding Mode=OneWay}" ... />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The idea here being that the implied DataContext of the DataTemplate will be the current list box item. By specifying a Binding with no Path, you're binding the text to the value "a", "b", or "c".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜