【问题标题】:How to set ItemsSource of a ComboBox, if it was present in an ItemsCotrol如何设置 ComboBox 的 ItemsSource,如果它存在于 ItemsCotrol 中
【发布时间】:2019-08-22 17:28:24
【问题描述】:

如果我们尝试将组合框保留在 ItemsCotrol 中,则不会显示组合框项目。 Please click here for understanding my requirement

我的要求是在 ItemsControl 中保留一个组合框,以便 ItemsControl 中包含 5 个组合框,并且每个组合框都将包含我们可以选择的项目集合。因此,为此我尝试使用以下代码并能够在 ItemsControl 中获取组合框,但组合框集合已被填充,请提供任何建议或解决方法..

<xamDataPresenter:Field Label="Reqs" BindingType="Unbound" Row="0" Column="4">
                        <xamDataPresenter:Field.CellValuePresenterStyle>
                            <Style TargetType="{x:Type xamDataPresenter:CellValuePresenter}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type xamDataPresenter:CellValuePresenter}">
                                            <ItemsControl Name="I" ItemsSource="{Binding Path=DataItem.CollectionCount}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <ComboBox ItemsSource="{Binding Path=DataItem.Collection}"/>
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </xamDataPresenter:Field.CellValuePresenterStyle>
                    </xamDataPresenter:Field>

【问题讨论】:

  • 您在组合框中缺少 ItemsSource?
  • 您的组合框代码没有多大意义。为什么要创建一个 TextBlock 作为 ComboBoxItem,而不是向 Combobox 提供一组项目(类似于您对 ItemsControl 所做的)。由于目前,您唯一的项目实际上是一个 TextBlock 元素本身(该项目不是 Polarity 属性提供的对象!),您需要为 ComboBox.SelectedItem 提供该 TextBlock 元素以选择它;如果您考虑一下,这当然也是相当不切实际的..
  • 呃....这应该做什么:&lt;ComboBox SelectedItem="{Binding Item, Mode=TwoWay}"/&gt;?对您的代码进行此编辑并没有什么意义......
  • 您是在说“每个组合框都有一个我们可以选择的项目集合”。所以,开始问自己:每个 ComboBox 的那些项目集合在哪里,因此,到达这些集合的绑定是什么样的?
  • 您链接的屏幕截图只显示了一些空的组合框。如果我所看到的只是一张空组合框的图片,而对它们的预期内容和目的没有任何解释,我怎么知道组合框的内容和目的应该是什么。请参阅我的第二条评论,其中我回复您说“每个组合框都将包含一组我们可以选择的项目”。

标签: wpf data-binding


【解决方案1】:

好的,我已经写了最基本的 ItemsControl 来尝试解释这些东西是如何工作的,希望你可以适应你对数据项的使用。

所以在您的窗口资源中,我创建了一个数据模板。这代表了一个重复步骤,并且将基于 DataItem。在这种情况下,我的 DataItem 有 2 个属性:DataItemProperty(string) 和 SelectedItem。无论您计划在组合框中显示什么,SelectedItem 都将具有相同的 DataType。

 <DataTemplate x:Key="StepTemplate">
    <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
        <TextBlock Text="{Binding Path=DataItemProperty}" Grid.Column="0"/>
        <ComboBox Grid.Column="1" ItemsSource="{Binding Path=DataContext.ItemsToSelectFrom, Mode=OneWay, RelativeSource={RelativeSource AncestorType=Window}}"
                          SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</DataTemplate>

此示例中的组合框将从/viewmodel 后面的代码而不是 DataItem 中获取可用选项的列表,但是当您选择某些内容时,它会更新 DataItem 上的 SelectedItem 属性。 然后展示你的物品:

  <ItemsControl 
        Focusable="False"
        ItemTemplate="{StaticResource StepTemplate}"
        ItemsSource="{Binding Path=Steps, Mode=OneWay}" />

所以 Steps 是我的代码隐藏/视图模型中的一个属性,它将确定显示多少“行”。 itemsControl 允许您轻松添加重复的数据集,而无需多次编写相同的 xaml。 希望有帮助吗?

【讨论】:

  • 两点:1.“我的代码隐藏/视图模型中的属性”——您的 XAML 未绑定到代码隐藏属性,两者不同。 2. 您不需要将Mode=OneWay, UpdateSourceTrigger=PropertyChanged 绑定到无法更新源的属性上。
  • 它绑定到后面的代码。我只是没有把它全部写出来,因为我没有为你做所有事情。你也需要自己思考,否则你学不会。
  • 我不是 OP。我是一位专家,试图帮助您改进一个表明对材料理解不佳的答案。您是否假设代码将设置 DataContext = this?这是非常糟糕的做法。
猜你喜欢
  • 1970-01-01
  • 2016-06-07
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 2018-11-08
相关资源
最近更新 更多