简体   繁体   English

组合框将ItemsSource绑定到与DataGrid相同的对象时,无法向DataGrid添加新行

[英]Can't add new row to DataGrid, while it has combobox that bind ItemsSource to same object as DataGrid

the concept is,I want a field in record link to other record primarykey 概念是,我想要记录中的字段链接到其他记录主键
so I have ComboBox in "DataGrid.RowDetailsTemplate" 所以我在“ DataGrid.RowDetailsTemplate”中有ComboBox
and bound that ComboBox.ItemsSource to same as DataGrid.ItemsSource 并将该ComboBox.ItemsSource绑定到与DataGrid.ItemsSource相同的对象
my source code are this 我的源代码是这个

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

and the code behind 和后面的代码

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

the program can showup ComboBox items properly, I can change selected item the problem is when I try to add new row to DataGrid it said 该程序可以正确显示ComboBox项目,我可以更改所选项目,问题是当我尝试向DataGrid添加新行时

在此处输入图片说明

any idea? 任何想法?

Thank you 谢谢

I don't think it's related to the ComboBox. 我不认为它与ComboBox有关。 You can't insert a new row into the table if doing so creates a null PK. 如果这样做会创建一个空PK,则不能在表中插入新行。 Make it an identify column. 使其成为标识列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM