我的 ItemsSource 语法正常吗?

发布于 2024-07-13 15:34:30 字数 958 浏览 4 评论 0原文

我有以下 XAML。 假设 FruitList 是 Fruits 的集合,每个 Fruits 都有一个 FruitSeed 的集合。 是否有语法将 cbxFruitSeeds 绑定到 FruitSeeds 集合,具体取决于 cbxFruits 中选择的 Fruit?

<GridView>

    <GridViewColumn Header="Fruits">

        <GridViewColumn.CellTemplate>

            <DataTemplate>

                <ComboBox DisplayMemberPath="Fruit.Name" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.FruitList}" x:Name="cbxFruits"/>

            </DataTemplate>

    </GridViewColumn.CellTemplate>

</GridViewColumn>

    <GridViewColumn Header="FruitSeeds">

        <GridViewColumn.CellTemplate>

            <DataTemplate>

                <ComboBox x:Name="cbxFruitSeeds"></ComboBox>

            </DataTemplate>

        </GridViewColumn.CellTemplate>

    </GridViewColumn>

</GridView>

I have the following XAML. Let's say FruitList is a collection of Fruits, each of which has a collection of FruitSeeds. Is there a syntax to bind cbxFruitSeeds to a collection of FruitSeeds, depending on which Fruit is selected in cbxFruits?

<GridView>

    <GridViewColumn Header="Fruits">

        <GridViewColumn.CellTemplate>

            <DataTemplate>

                <ComboBox DisplayMemberPath="Fruit.Name" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.FruitList}" x:Name="cbxFruits"/>

            </DataTemplate>

    </GridViewColumn.CellTemplate>

</GridViewColumn>

    <GridViewColumn Header="FruitSeeds">

        <GridViewColumn.CellTemplate>

            <DataTemplate>

                <ComboBox x:Name="cbxFruitSeeds"></ComboBox>

            </DataTemplate>

        </GridViewColumn.CellTemplate>

    </GridViewColumn>

</GridView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖果控 2024-07-20 15:34:30

看起来您正在尝试做一个级联组合框。

真的没有吗如何在 xaml 绑定中跟踪数据集父关系? 展示了如何在 xaml 中进行父子设置。

It looks like you're trying to do a cascading combobox.

Is there really no way to follow up dataset parent relation in xaml binding? shows how to do a parent-child set up in xaml.

薄暮涼年 2024-07-20 15:34:30

有两种方法可以正确地做到这一点。

  1. 将集合视图设置为第二个组合框的源,然后设置第一个组合框以更改该集合视图上的过滤器。
  2. 让第一个组合框的项目的数据类型每个都包含第二个组合框的项目列表。 当选择第一个组合框时,其结果将设置为第二个组合框的数据上下文,然后将第二个组合框绑定到包含的列表。

第一个是更好的选择,因为您不需要将水果类型存储在保存用户选择的数据中。 第一个组合框由配置或元数据填充。

Two ways to do this the right way.

  1. Setup a collection view as the source for the second combobox, then setup the first combobox to alter the filter on that collection view.
  2. Have the datatype for the items for the first combobox each contain a list of items for the second combobox. When the first combobox is selected, its result is set as the second's data context, then bind the second to the contained list.

The first is preferable because you don't need to store the fruit type in the data that saves the user's choice. The first combobox is populated by a config, or metadata.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文