我正在向绑定到数据网格的数据源添加一个项目,但 UI 没有更新以显示新项目。这是我的代码。
DBContainer ctx = new DBContainer();
private void btnAddNewDesignator_Click(object sender, RoutedEventArgs e)
{
DESIGNATOR a = new DESIGNATOR();
a.DesignatorName = txtDesignator.Text;
a.TXFreq = txtTX.Text;
a.RXFreq = txtRX.Text;
ctx.AddToDESIGNATORs(a);
ctx.SaveChanges();
dgDesignators.Items.Refresh();
}
private void btnAddNewDesignator_Loaded(object sender, RoutedEventArgs e)
{
dgDesignators.ItemsSource = ctx.DESIGNATORs;
}
这是我的xml:
<DataGrid AutoGenerateColumns="False" Height="225" HorizontalAlignment="Left" Margin="56,230,0,0" Name="dgDesignators" VerticalAlignment="Top" Width="602" CanUserAddRows="True" CanUserDeleteRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Designator" Binding="{Binding Path=DesignatorName,Mode=OneWay}" />
<DataGridTextColumn Header="TX Frequency" Binding="{Binding Path=TXFreq,Mode=OneWay}" />
<DataGridTextColumn Header="RX Frequency" Binding="{Binding Path=RXFreq,Mode=OneWay}" />
<DataGridTextColumn Header="In Use" />
</DataGrid.Columns>
</DataGrid>
如您所见,我正在刷新,我也尝试过先将ItemSource设置为null,然后重新绑定它,但无济于事。谢谢你的帮助。