【问题标题】:Add a new row to a WPF DataGrid that is bound to an XML file将新行添加到绑定到 XML 文件的 WPF DataGrid
【发布时间】:2015-06-21 08:25:09
【问题描述】:

我正在尝试使用户能够将新行/数据添加到双向绑定到 XML 文件的 DataGrid。我可以编辑所有值,但无论我尝试了什么,网格末尾的空白行都不会显示。

我主要在 XAML 中尝试完成此任务,并使用最少的代码。我也不希望用户必须单击按钮来添加行(我可以完成)。

这是我的 DataGrid 的一个示例。它使用 0 代码更新我的 XML 文件

<DataGrid  Name="masterCrewGrid" CanUserAddRows="True" CanUserDeleteRows="True" DataContext="{StaticResource CrewInfo}" ItemsSource="{Binding XPath=/Names/Name}" AutoGenerateColumns="False" CellEditEnding="masterCrewGrid_CellEditEnding" >
      <DataGrid.Columns>
            <DataGridTemplateColumn Header="Active">
                 <DataGridTemplateColumn.CellTemplate>
                       <DataTemplate>
                            <CheckBox IsChecked="{Binding XPath=@isActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                      </DataTemplate>
                 </DataGridTemplateColumn.CellTemplate>
             </DataGridTemplateColumn>
             <DataGridTextColumn Header="Family Name" Binding="{Binding XPath=Family, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
             <DataGridTextColumn Header="First Name" Binding="{Binding XPath=First, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
             <DataGridTextColumn Header="Middle Name" Binding="{Binding XPath=Middle, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
             <DataGridTextColumn Header="Birthday" Binding="{Binding XPath=Birthday, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
             <DataGridTextColumn Header="Crew Position" Binding="{Binding XPath=CrewPosition, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
      </DataGrid.Columns>
</DataGrid>

谁能解释一下为什么新行不会出现?是否与绑定到 XML 文件的网格有关?

【问题讨论】:

    标签: c# xml wpf xaml datagrid


    【解决方案1】:

    我猜当您直接绑定到 XML 文件时,它是只读的。 获取一个 XMLDataProvider 并绑定到它。然后您可以使用 RowEditEnding 事件进行修改:

    点击此处了解更多信息:

    How do I create a new row in WPF DataGrid when it is bound to an XmlDataProvider?

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 2011-05-14
      • 2013-06-01
      • 2014-10-23
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      相关资源
      最近更新 更多