0

我正在向绑定到数据网格的数据源添加一个项目,但 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,然后重新绑定它,但无济于事。谢谢你的帮助。

4

2 回答 2

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();
}

项目来源属性:

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 :窗口,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
于 2013-09-09T19:07:10.097 回答
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

于 2013-09-09T19:08:15.247 回答