C# CanUserAddRows新行未保存在DataGrid中

C# CanUserAddRows新行未保存在DataGrid中,c#,.net,wpf,C#,.net,Wpf,已创建DataGrid并设置CanUserAddRows=True 在cs文件中有一个保存更新的按钮: private void Save_Click(object sender, RoutedEventArgs e) { UnitService unitService = new UnitService(); unitService.SaveUpdates(valuationCase); MainWindow mainWin = new MainWindow();

已创建DataGrid并设置CanUserAddRows=True

在cs文件中有一个保存更新的按钮:

private void Save_Click(object sender, RoutedEventArgs e)
{
    UnitService unitService = new UnitService();

    unitService.SaveUpdates(valuationCase);

    MainWindow mainWin = new MainWindow();
    mainWin.Show();
    this.Close();
}
窗口上的datagrid中还有一个不可编辑的文本框,该文本框可以使用保存单击按钮正确保存编辑。只是新行不是

有什么想法吗

数据网格定义:

    <DataGrid Name="dgCommentsList" AutoGenerateColumns="False" Margin="10,196,9.953,38.204" CanUserAddRows="True" FontSize="18">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="FontSize" Value="20" />
                <Setter Property="FontWeight" Value="bold" />
            </Style>
        </DataGrid.ColumnHeaderStyle>
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Type" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox x:Name="Type" Text="{Binding Type}" >
                            <TextBox.Style>
                                <Style TargetType="TextBox">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsReadOnly}" Value="False">
                                            <Setter Property="TextBox.IsReadOnly" Value="False"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding IsReadOnly}" Value="True">
                                            <Setter Property="TextBox.IsReadOnly" Value="True"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

    </DataGrid>

您应该使用dataGrid.CommittedIt提交行上的编辑

编辑:诊断问题后,这里开始

您需要在DataContext类上实现INotifyPropertyChanged,即:Viewmodel,如下所示:

 public class ViewModel: INotifyPropertyChanged
 {
            private string _type;
            public string Type 
            {
                get { return _type; } 
                set 
                {
                    _type = value;
                    OnPropertyChanged("Type");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
}
或者从DependencyObject扩展并使用依赖项属性,如下所示:

 public class ViewModel: DependencyObject
 {

            public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
                "Type", typeof (string), typeof (ViewModel), new PropertyMetadata(default(string)));

            public int Type
            {
                get { return (int) GetValue(TypeProperty ); }
                set { SetValue(TypeProperty , value); }
            }
 }

希望有帮助

我认为您需要设置绑定模式,以便将其写回底层对象。另外,我注意到您的DataGrid没有ItemsSource。我猜这只是你漏掉的一个片段。


你能发布上面添加的datagrid定义吗。它包括一个用于绑定IsReadOnly属性的变通方法,但该部分可以工作我没有任何设置行,因此我应该在代码中的何处添加它??在“保存”单击处理程序中调用dgCommentsList.CommittedIt。它将提交您的编辑。这将触发行上的EditEnding事件。您可以通过编辑的行处理这些事件,但它不起作用。这可能值得一提——我在另一个分支上使用了很多未版本化的文件,并在没有额外代码的情况下成功了。我把那个版本弄丢了,现在它在这里不起作用了。不知道我是否理解正确。你的意思是,你尝试了我在另一个分支上提供的解决方案,它成功了,但现在没有?我说得对吗?如果是这样,您应该检查更改中引入了哪些可能导致操作失败的内容。不,不是您的更改。我的原始代码以前工作过。唯一的变化是没有版本控制的文件,所以我不明白为什么它现在不起作用。不过,您的解决方案也不适用于银行评论,但这也不适用。另外,当我使用一个简单的列定义时,就像上面答案的最新注释一样,我所要做的就是按Enter键,出现一个新行并保存上一行。使用“我的绑定文本框”时,“输入”按钮不执行任何操作。不知道这个helpsUpdate:我在IsReadOnly的DataTrigger绑定上尝试了Mode=TwoWay,当我运行代码时,它返回了一个错误,指出:TwoWay或OneWayToSource绑定不能在只读属性'IsReadOnly'上工作,不知道这是否相关。Mode=TwoWay只能在可以读写的属性上工作,即:prop{get;set;}
<TextBox x:Name="Type" Text="{Binding Type, Mode=TwoWay}">