从集合绑定到 DataGridComboBoxColumn

发布于 2024-08-20 16:35:56 字数 1654 浏览 4 评论 0原文

尝试绑定到 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 路绑定,双击名字列会弹出组合框,其中包含集合中所有可能的名字的选项(即乔治、吉姆和艾米)。

感谢任何帮助!

Trying to bind to a collection in WPF, I got the following to work:

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;
        }
    }
}

unnessecary probably but here is Person class:

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;
        }
    }
}

But what I really need is this in DataGridComboBoxColumn 's. Here are my revisions:

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#:

Stays same.

Problem now is that I get empty combobox columns! Any ideas how I can get this to work?

In the long run I need 2 way binding, where a double click on the firstname column brings up the comobo box which then holds the options of all possible first names in the collection (i.e. George, Jim and Amy).

Grateful any assistance!

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

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

发布评论

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

评论(1

丑疤怪 2024-08-27 16:35:56

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 不起作用

但是,为 将组合框与 WPF DataGrid 结合使用。最后,您可能想看看这篇文章 DataGrid 带来更多乐趣,作者:Margaret Parsons。

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

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

The DataGrid needs to have the Header and ItemsSource Properties set:

<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>

It appears there was in issue in one of the releases of the toolkit when using the DataGridComboBoxColumn.ItemsSource: DataGridComboBoxColumn.ItemsSource doesn't work.

However, there was a work-around created for Using combo boxes with the WPF DataGrid. Finally, you may want to take a look at the article More fun with DataGrid by Margaret Parsons as well.

Edit
Now I'm not so sure the above code works. I did that from memory and referenced the other links as resources.

Take a look at this SO post which appears to address this problem: Problem binding DataGridComboBoxColumn.ItemsSource

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