【问题标题】:how to add a ComboBoxItem to a ComboBox in a ListBox (both use databinding)如何将 ComboBoxItem 添加到 ListBox 中的 ComboBox(都使用数据绑定)
【发布时间】:2013-11-01 20:30:52
【问题描述】:

我无法找到解决方案。我有一个 ListBox,它的 DataTemplate 有一个 ComboBox。 DataBinding 就位,这是一种集合场景的集合。我想为所有组合框预先设置一个“选择一个项目”。我该怎么做?

编辑:真的不确定为什么您需要 code/xaml 来解决上述问题。但无论如何都在下面:

<Resources>
<ResourceDictionary>
            <DataTemplate x:Key="CategoriesDataTemplate">
                <StackPanel Orientation="vertical">
                    <TextBlock Text="{Binding Path=CategoryName}"></TextBlock>
                    <ComboBox ItemsSource="{Binding Path=Products}" Background="Transparent" SelectedValuePath="ProductId" DisplayMemberPath="ProductName">
                    </ComboBox>
                </StackPanel>
            </DataTemplate>
</ResourceDictionary>
</Resources>
.....
<Grid..>
                <ListBox ItemsSource="{Binding Categories}" ItemTemplate="{DynamicResource CategoriesDataTemplate}">
</Grid>

对于每个类别,我将在下面显示类别名称和其产品的组合框。用户可以为每个类别选择一种产品。对于每个这样的组合框,我希望第一项是“选择产品”或类似的东西。 注意:我正在寻找是否有一种方法可以做到这一点,而无需在每个类别中的每个产品集合中预先添加一个项目(如果可能,我不希望弄乱源集合)。某种事件处理方法?

【问题讨论】:

  • 代码、Xaml 什么的?
  • @StefanDenchev 好的,我现在添加了部分 Xaml。轮到你了:)
  • “真的不知道为什么你需要代码/xaml 来解决上述问题。”:因为它有助于理解......如果我理解正确,this 应该足够了......哈哈,另外两个“布赖恩参与了那个问题...
  • @StefanDenchev 我想我的编辑真的没有帮助。如果我正确理解您的链接,它会谈论显示文本,而不是“添加”额外的项目(我可能错了,我是 wpf 的新手)。我想预先挂起一个项目,而不仅仅是设置 text 属性。我管理的解决方案如下
  • 是的,我认为只显示它而不是一开始就显示空字段就足够了......很高兴你明白了。

标签: wpf wpf-controls


【解决方案1】:

经过进一步挖掘,得到了结合的解决方案:

hbarc's suggestionShimmy's solutionkmatyaszek's answer。诀窍是使用 CompositeCollection,因此我们可以为 ComboBox 使用静态和动态项

我的 ComboBox 现在看起来像这样(我无法让它与 StackPanel 资源一起使用。我是 wpf 的新手,请对 StackPanel 资源方法发表评论。):

                <StackPanel.Resources>
                    <CollectionViewSource Source="{Binding Path=Products}" x:Key="options"/>
                    <!--not used. doesn't seem to be working when used DummyOption is a property in ViewModel-->
                    <CollectionViewSource Source="{Binding DummyOption}" x:Key="dummyOption"/>
                </StackPanel.Resources>

                <ComboBox Background="Transparent" SelectedValuePath="ProductId" DisplayMemberPath="ProductName" SelectedIndex="0">
                    <ComboBox.ItemsSource>
                        <CompositeCollection>
                            <!--works-->
                            <models:Product ProductName="Select" ProductId="{x:Static sys:Guid.Empty}" .../>
                            <!--notworking-->
                            <!--<ComboBoxItem Content="{Binding dummyOption}" />-->
<!--notworking-->
<!--<ComboBoxItem Content="{Binding DummyOption}" />-->
                            <!--notworking-->
                            <!--<ComboBoxItem Content="{Binding Source={StaticResource ResourceKey=dummyOption}}" />-->
                            <CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=Products}}" />
                        </CompositeCollection>
                    </ComboBox.ItemsSource>    
                </ComboBox>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多