【发布时间】:2019-12-03 13:31:47
【问题描述】:
您好,我创建了这个测试代码:
<Button x:Name="b_Delete" Content="Delete" Margin="10" Grid.Row="1"/>
<Button x:Name="b_Save" Content="Save" Margin="10" Grid.Row="1"
Grid.Column="1" Command="{Binding Path=SaveCommand}"/>
<DataGrid x:Name="dataGrid" Margin="10" Grid.ColumnSpan="2"
ItemsSource="{Binding Path=Books, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Author" Header="Author"
Width="*" Binding="{Binding author, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="Title" Header="Title"
Width="*" Binding="{Binding title}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="Price" Header="Price"
Width="*" Binding="{Binding price}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTemplateColumn x:Name="Delete_Row" Header="Delete" Width="0.2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="X"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我创建了两个按钮和一个DataGrid。 DataGrid 是用读入数据库的值填充,现在如果用户将文本更改为单元格,则新值将保存到数据库中(按钮 X 删除到行中暂时没有实现)。
我的想法是制作一个ObservableCollection where I add the information of row changed (another ObservableCollectfor row deleted) and When the button Save is clicked, I save the value into Database taking the value intoObservableCollect`。你怎么看?
如何截取DataGrid单元格中值的变化?
private ObservableCollection<Book> books = null;
public ObservableCollection<Book> Books
{
get { return books; }
set
{
books = value;
NotifyPropertyChanged();
}
}
如果我更改单元格值,这点不是调用?
感谢您的帮助。
【问题讨论】: