【问题标题】:How can I bind the content of the combobox in xaml如何在 xaml 中绑定组合框的内容
【发布时间】:2017-04-29 00:53:54
【问题描述】:
<ComboBox Grid.Row="1" Margin="0,0,0,175" DataContext="{Binding }" />

我已将 xaml 的 DataContext 设置为一个类。

在其中我有一个名为Player 的类。 Player 有一个名为Quests 的另一个对象的属性数组。每个Quest 都有一个name 属性。

如何将组合框的内容绑定到Quest 数组,并让每个项目由其name 属性表示?

我假设我将不得不使用绑定

【问题讨论】:

标签: c# wpf data-binding combobox binding


【解决方案1】:
  1. 语句DataContext="{Binding }" 只是获取DataContext 属性的当前值并将其分配回去——这不会产生任何影响。

  2. 假设这个视图的DataContext有一个名为Quests的属性,你需要做的是:

    <ComboBox Grid.Row="1" Margin="0,0,0,175" ItemsSource="{Binding Quests}" />
    

请注意,ComboBox 派生自 ItemsControl,因此设置 ItemsSource 属性始终可以解决问题。

您可能需要为任务项目创建一个数据模板才能查看实际感兴趣的内容。 This should get you started with Data Templates.

创建数据模板后,使用ItemTemplate 属性将其设置为组合框(再次感谢ItemsControl)。

【讨论】:

    【解决方案2】:

    没关系。发现:D

    如果其他人有同样的问题:

    假设您的窗口/页面中有一个名为 Foos 的列表。 Foo 有一个属性名称。现在您在 XAML 中设置绑定,如下所示:

    <ComboBox ItemsSource="{Binding Path=Foos}"
    DisplayMemberPath="Name"
    SelectedValuePath="Name"
    

    引用自WPF Databinding combobox to a list<string>

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多