【问题标题】:Trying to add a combobox within datagrid which has an itemsource already尝试在已经有 itemsource 的 datagrid 中添加一个组合框
【发布时间】:2017-11-28 23:18:00
【问题描述】:

如果我自己有一个带有项目来源的组合框,例如:

<ComboBox ItemsSource="{Binding Combobox.Options}"/>

这很好用。其中 Combobox.Options 是字符串列表 {"option1","option 2",... etc}

但是,我希望在具有项目源的数据网格中有一个组合框:

例如:

<DataGrid CanUserAddRows="False"  AutoGenerateColumns="False" SelectionMode="Single" ItemsSource="{Binding Users.Table.Tables[0]}">

但是当我将它添加到数据网格模板列中时,我总是得到一个空的组合框。我也试过 Find Ancestor Relative soure 但也得到了一个空的组合框。

【问题讨论】:

    标签: c# wpf mvvm combobox datagrid


    【解决方案1】:

    终于找到了问题所在。获取数据网格中的项目源列表时需要添加DataContext:

    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}"/>
    

    【讨论】:

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