我使用.NET 3.5和WPF Toolkit进行DataGrid控件。我希望datagrid显示一个项集合,以及一个组合框列来显示集合中的一个项目。我可能在绑定方面做错了,但是当我运行它时,组合框列是空白的,但是我可以从组合框中选择一个项目然后显示它。任何帮助非常感谢,谢谢!
我的XAML:
<tk:DataGrid Name="MyDataGrid" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
<tk:DataGridComboBoxColumn x:Name="MyComboBox" Header="Member" SelectedItemBinding="{Binding MyMember}"/>
</tk:DataGrid.Columns>
</tk:DataGrid>
我的代码:
class Member
{
public string Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return Id + " - " + Name;
}
}
class Data
{
public string Description { get; set; }
public Member MyMember { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var list = new List<Member>()
{
new Member() { Id = "01", Name = "aaa" },
new Member() { Id = "02", Name = "bbb" },
new Member() { Id = "03", Name = "ccc" }
};
MyComboBox.ItemsSource = list;
var datalist = new List<Data>()
{
new Data() { Description = "abc", MyMember = new Member() { Id = "01", Name = "aaa" } },
new Data() { Description = "def", MyMember = new Member() { Id = "02", Name = "bbb" } },
new Data() { Description = "ghi", MyMember = new Member() { Id = "03", Name = "ccc" } }
};
MyDataGrid.ItemsSource = datalist;
}
}
答案 0 :(得分:0)
var list = new List<Member>()
{
new Member() { Id = "01", Name = "aaa" },
new Member() { Id = "02", Name = "bbb" },
new Member() { Id = "03", Name = "ccc" }
};
**Member _me = new Member() { Id = "04", Name = "ddd" };
list.Add(_me);**
MyComboBox.ItemsSource = list;
var datalist = new List<Data>()
{
new Data() { Description = "abc", **MyMember = _me** },
new Data() { Description = "def", MyMember = new Member() { Id = "02", Name = "bbb" } },
new Data() { Description = "ghi", MyMember = new Member() { Id = "03", Name = "ccc" } }
};
MyDataGrid.ItemsSource = datalist;
在向列表中添加项目时进行了少量更改。添加的项目与分配所选项目的内容不同。