【发布时间】:2012-12-25 11:21:34
【问题描述】:
我有一个StackPanel,其中DataGrid 绑定到DataSet,Grid 位于StackPanel 底部的DataGrid 下方。 Grid 包含一个Button,在用户更改DataGrid 的内容后,我想用它来更新与DataSet 关联的DataAdapter。
这主要按预期工作,但并非总是如此。
我的期望是每当我单击按钮时都会调用事件处理程序。如果我更改数据网格中的现有行然后单击按钮,或者如果我在网格的最后(空)行中输入值,然后按 enter 并最后单击按钮,这确实有效。如果我在网格的最后一行输入值并且不按 Enter,则单击该按钮会在数据网格的底部产生一个新的(空)行,并且事件处理程序被 not 调用。仅通过再次单击它来调用事件处理程序。为什么是这样?我可以更改吗?
我的第一个怀疑是 DataSetsomehow 需要被告知新数据,但如果我添加一个新行,按 Enter 并添加第二个新行,那么按钮单击也不会导致调用事件处理程序,这意味着我的怀疑没有解释这种行为。
这是使用 Visual Studio Express 2012
这里是XAML:
<Window x:Class="AppVer0._01.ProducerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Producer" Height="300" Width="300">
<StackPanel>
<DataGrid ItemsSource="{Binding producer}"
AutoGenerateColumns="True"
HorizontalAlignment="Left"
Name="producerDataGrid"
/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Button Name="buttonUpdate" Click="buttonUpdate_Click_1">Aktualisieren</Button>
</Grid>
</StackPanel>
</Window>
(我计划添加更多按钮,因此网格有更多列)后面的代码(用于`Window)定义绑定如下:
DataSet ds = new DataSet();
// ...
// load data from adapter into dataset
//
this.DataContext = ds;
当然还有定义的事件处理程序。
编辑:在重要的情况下,窗口被称为对话框。
【问题讨论】:
标签: c# wpf xaml click wpfdatagrid