概念是,我希望记录链接到其他记录主键的字段
所以我在“DataGrid.RowDetailsTemplate”中有ComboBox
并将ComboBox.ItemsSource绑定到与DataGrid.ItemsSource相同的内容
我的源代码是这个
<DataGrid Name="CollectionGrid" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Column1" Binding="{Binding ID}"/>
<DataGridTextColumn Header="Column1" Binding="{Binding Name}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border Background="Gray" CornerRadius="10">
<ComboBox ItemsSource="{Binding ElementName=CollectionGrid, Path=ItemsSource}" DisplayMemberPath="Name" SelectedValuePath="Value" SelectedValue="{Binding Ref, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Border>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
和背后的代码
public partial class MainWindow : Window
{
public ObservableCollection<PACKET> DB { set; get; }
public MainWindow()
{
InitializeComponent();
DB = new ObservableCollection<PACKET>();
DB.Add(new PACKET { ID = 1, Name = "TEST1", Ref = 1});
DB.Add(new PACKET { ID = 2, Name = "TEST2", Ref = 1 });
DB.Add(new PACKET { ID = 3, Name = "TEST3", Ref = 1 });
CollectionGrid.ItemsSource = DB;
}
}
public class PACKET
{
public Int64 ID { set; get; }
public string Name { set; get; }
public Int64 Ref { set; get; }
}
程序可以正确显示ComboBox项目, 我可以更改所选项目 问题是当我尝试向DataGrid添加新行时 它说
任何想法?
谢谢
答案 0 :(得分:0)
我认为它与ComboBox无关。如果这样做会创建一个空PK,则无法在表中插入新行。将其设为标识列。