我正在向绑定到数据网格的数据源添加项目,但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;
}
这是我的xaml:
<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,然后重新绑定它,但无济于事。谢谢你的帮助。
答案 0 :(得分:0)
按钮事件处理程序:
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();
designatorDataSource = ctx.DESIGNATOR.ToList();
}
ItemsSourceProperty:
private List<DESIGNATOR> _designatorDataSource = new List<DESIGNATOR>();
public List<DESIGNATOR designatorDataSource {
get {
return _designatorDataSource;
}
set {
_designatorDataSource = value;
OnPropertyChanged("designatorDataSource");
}
}
然后绑定DataGrid:
<DataGrid Name="dgDesignators" ItemsSource={Binding designatorDataSource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
</DataGrid>
编辑:
添加INotifyPropertyChanged
,让您的课程延长INotifyPropertyChanged
。
实施例: 使用System.ComponentModel; 公共类MyClass:Window,INotifyPropertyChanged
添加属性更改事件
#region Property Change Event
/// <summary>
/// Occurs when a property is changed
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the <see cref="PropertyChanged"/> for a given
/// property.
/// </summary>
/// <param name="propertyName"></param>
protected void OnPropertyChanged(String propertyName) {
// Get the handler
PropertyChangedEventHandler handler = this.PropertyChanged;
// Check that the event handler is not null
if(null != handler) {
// Fire the event
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
答案 1 :(得分:0)
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.ToList();
}
我不建议在WPF中使用这种代码,尝试在MVVM模式中执行它并且它应该更容易实现,您需要做的就是创建一个类,它将充当DataGrid的DataContext并生成该类实现 INotifyPropertyChange