【问题标题】:Other ItemsSource in WPFWPF 中的其他项目来源
【发布时间】:2011-05-25 09:32:25
【问题描述】:

我有一个DataGrid dg;,我想显示一个DataGridComboBoxColumn,其中包含从某个列表填充的数据(使用绑定)。使用 XAML 我已经正确设置了dg.ItemsSource,但我不知道如何设置列的ItemsSource

<DataGridComboBoxColumn ItemsSource={?????????} />

詹姆斯

【问题讨论】:

  • 您能发布一些您收藏的代码吗?为什么你不能直接访问它?

标签: c# .net wpf data-binding


【解决方案1】:

您可以使用 datacontext 来获取要绑定到 DataGridComboBoxColumn 的属性。查看下面的帖子

Binding a WPF DataGridComboBoxColumn with MVVM

【讨论】:

    【解决方案2】:
    <DataGridComboBoxColumn ItemsSource="{Binding AvailableOptions}" />
    

    这里,AvailableOptions 是绑定到网格行的数据项的属性。此属性应包含组合框中可用的选项列表。

    更新:

    如果要从中选择的选项集合是固定的,您可以使用资源中定义的ObjectDataProvider。例如,请参阅http://blogs.ugidotnet.org/ccavalli/archive/2006/02/09/34592.aspx

    在你的情况下,它可能是这样的:

    <ObjectDataProvider x:Key="Options" ObjectType="{x:Type my:MyOptions}"  />
    

    -

    <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource Options}}" />
    

    【讨论】:

    • 不,这不是那么简单,因为我想用一些固定的集合填充此列(每一行),但我无法直接访问它。我需要以某种方式更改上下文,但我不知道如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多