c# - 从集合绑定(bind)到 DataGridComboBoxColumn

标签 c# wpf data-binding wpfdatagrid datagridcomboboxcolumn

尝试绑定(bind)到 WPF 中的集合,我得到了以下工作:

XAML:

<toolkit:DataGrid Name="dgPeoples"/>

CS:

namespace DataGrid
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1
    {
        private readonly ObservableCollection<Person> personList = new ObservableCollection<Person>();

        public Window1()
        {
            InitializeComponent();

            personList.Add(new Person("George", "Jung"));
            personList.Add(new Person("Jim", "Jefferson"));
            personList.Add(new Person("Amy", "Smith"));

            dgPeoples.ItemsSource = personList;
        }
    }
}

可能没必要,但这是 Person 类:

namespace DataGrid
{
    public class Person
    {
        public string fName { get; set; }
        public string lName { get; set; }

        public Person(string firstName, string lastName)
        {
            fName = firstName;
            lName = lastName;
        }
    }
}

但我真正需要的是 DataGridComboBoxColumn 中的这个。这是我的修改:

XAML:

<toolkit:DataGrid Name="dgPeoples" Grid.Row="0" AutoGenerateColumns="False">
    <toolkit:DataGrid.Columns>
        <toolkit:DataGridComboBoxColumn Width="5*"/>
        <toolkit:DataGridComboBoxColumn Width="5*"/>
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

C#:

保持不变。

问题 现在是我得到空的组合框列!我有什么想法可以让它发挥作用吗?

从长远来看,我需要 2 种方式绑定(bind),双击名字列会弹出组合框,然后包含集合中所有可能名字的选项(即 George、Jim 和 Amy)。

感谢任何帮助!

最佳答案

DataGrid 需要设置HeaderItemsSource 属性:

<toolkit:DataGrid Name="dgPeoples" Grid.Row="0" AutoGenerateColumns="False">
    <toolkit:DataGrid.Columns>
        <toolkit:DataGridComboBoxColumn Width="5*"
            Header="First Name"
            ItemsSource="{Binding Path=fName}"/>
        <toolkit:DataGridComboBoxColumn Width="5*"
            Header="First Name"
            ItemsSource="{Binding Path=lName}"/>
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

使用 DataGridComboBoxColumn.ItemsSource 时,其中一个工具包版本似乎存在问题:DataGridComboBoxColumn.ItemsSource doesn't work .

但是,为 Using combo boxes with the WPF DataGrid 创建了一个解决方法.最后,你可能想看看这篇文章More fun with DataGrid玛格丽特·帕森斯 (Margaret Parsons) 也是如此。

编辑
现在我不太确定上面的代码是否有效。我凭内存做到了这一点,并将其他链接作为资源引用。

看看这个似乎解决这个问题的 SO 帖子:Problem binding DataGridComboBoxColumn.ItemsSource

关于c# - 从集合绑定(bind)到 DataGridComboBoxColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2225701/

相关文章:

c# - File.Copy from Unc path to (same server's) Unc路径查询

c# - Directshow 过滤器访问线程

c# - WPF 组合框样式和触发器不起作用

wpf - OxyPlot 图表中的可绑定(bind)注释

wpf - DataGrid - 将自定义对象与子列表绑定(bind)

c# - 在 MVVMLight 中关于 ViewModelLocator 正确使用 MEF

c# - 将某些页面功能或用户界面限制为 Asp.Net 中经过身份验证的用户

wpf - 为 ContentPresenter 中的所有元素设置样式

c# - 获得焦点时突出显示 WPF 控件

c# - if 语句中的数据绑定(bind)