在WPF中使用datagrid从数据库添加数据。 datagrid XAML
<DataGrid ItemsSource="{Binding Children}" CanUserReorderColumns="True" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" HorizontalScrollBarVisibility="Hidden" HorizontalContentAlignment="Center" IsManipulationEnabled="True" AutoGenerateColumns="False" IsReadOnly="False">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="200"/>
<DataGridTextColumn Header="Middle Name" Binding="{Binding Path=MiddleName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="200"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="200"/>
</DataGrid.Columns>
</DataGrid>
将datagrid绑定到ObservableCollection。目标是添加数据,但是当我启动我的应用程序时,datagrid是空的,没有编辑的机会。
Viewmodel部分。
private ObservableCollection<Other> _children;
public ObservableCollection<Other> Children
{
get { return _children; }
set
{
_children = value;
OnPropertyChanged("Children");
}
}
EDM自动生成课程。
public partial class Other
{
public int id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
...
}
答案 0 :(得分:0)