ListBox中的WPF ComboBox文本

时间:2010-04-16 22:15:33

标签: wpf xaml mvvm listbox

我在列表框中有一个ComboBox列表,如下所示:

    <ListBox ItemsSource="{Binding Values}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ComboBox ItemsSource="{...}" IsTextSearchEnabled="True" IsEditable="True"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我的列表框中的值ItemsSouce在我的ViewModel中定义为

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

如何获取每个ComboBox的Text以显示该特定ListBox项的值?

(即如果值为{“a”,“b”,“c”}我想要一个显示“a”,“b”和“c”的3个组合框列表

1 个答案:

答案 0 :(得分:3)

试试这个。

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

这里的想法是DataTemplate的隐含DataContext将是当前列表框项。通过指定不带路径的绑定,您可以将文本绑定到值“a”,“b”或“c”。