从集合绑定到 DataGridComboBoxColumn
尝试绑定到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataGrid 需要设置Header
和ItemsSource
属性:看来其中之一存在问题使用
DataGridComboBoxColumn.ItemsSource
时工具包的版本:DataGridComboBoxColumn.ItemsSource 不起作用。但是,为 将组合框与 WPF DataGrid 结合使用。最后,您可能想看看这篇文章 DataGrid 带来更多乐趣,作者:Margaret Parsons。
编辑
现在我不太确定上面的代码是否有效。我凭记忆做到了这一点,并将其他链接作为资源引用。
看看这个似乎解决这个问题的SO帖子:问题绑定DataGridComboBoxColumn.ItemsSource
The DataGrid needs to have theHeader
andItemsSource
Properties set: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