开发者

WPF: Update Listbox automatically C#

I have two WPF windows developed using the surface SDK, one that is a data entry form, and the second dispays the data in a listbox. The listbox displays the data perfectly but when I add a new record using the data entry form, the listbox is not updated until I reopen the window. Is there a way to automatically update the listbox through binding or something?

This is the listbox code:

    <s:SurfaceListBox Height="673" Margin="0,26,0,31" Name="surfaceListBox1" ItemsSource="{Binding Path={}}" Width="490">
        <s:SurfaceListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Label Width="80" FontSize="8" Content="{Binding Path=item1}"></Label>
                    <Label Width="80" FontSize="8" Content="{Binding Path=item2}"></开发者_运维知识库Label>
                    <Label Width="210" FontSize="8" Content="{Binding Path=item3}"></Label>
                    <Label Width="80" FontSize="8" Content="{Binding Path=item4}"></Label>
                    <Label Width="60" FontSize="8" Content="{Binding Path=item5, Converter={StaticResource booleanconverter}}"></Label>
                </StackPanel>
            </DataTemplate>
        </s:SurfaceListBox.ItemTemplate>
    </s:SurfaceListBox>

I am using Visual C# 2008 and the code to fill the listbox is:

    private SHIPS_LOGDataSet ShipData = new SHIPS_LOGDataSet();
    private SHIPS_LOGDataSetTableAdapters.MAINTableAdapter taMain = new SHIPS_LOGDataSetTableAdapters.MAINTableAdapter();
    private SHIPS_LOGDataSetTableAdapters.TableAdapterManager taManager = new ShipsLogSurface.SHIPS_LOGDataSetTableAdapters.TableAdapterManager();

    private void SurfaceWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.taMain.Fill(this.ShipData.MAIN);
        this.DataContext = from MAIN in this.ShipData.MAIN orderby MAIN.MESSAGE_ID descending select MAIN;

    }

The only table in my database is called MAIN.

I'm guessing I might have to use a collection view or similar but don't know how to implement that. Any ideas would be much appreciated. Thanks


INotifyPropertyChanged is an interface which you should implement in your data class (ShipData?). The properties in your data class should look as follows:

private string _myField;
public string MyField { 
    get { return _myField; } 
    set { _myField = value; onPropertyChanged(this, "MyField"); } 
}

So whenever something in your data class changes (i.e. add/delete/update), it will fire the OnPropertyChanged event. Your List or ObservableCollection that you use to populate the list listens to this OnPropertyChanged event and will update itself whenever the event is fired.


Try to do it with INotifyPropertyChanged.


surfaceListBox1.Items.Refresh();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜