代码之家  ›  专栏  ›  技术社区  ›  lambinator

列表框内的wpf组合框文本

  •  1
  • lambinator  · 技术社区  · 15 年前

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

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

    在我的viewmodel中,我的listbox itemsouce的值定义为

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

    如何获取每个组合框的文本以显示特定列表框项的值?

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

    1 回复  |  直到 15 年前
        1
  •  3
  •   Josh    15 年前

    试试这个。

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

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

    推荐文章