views:

319

answers:

3

Hi,

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

+1  A: 

Try to do it with INotifyPropertyChanged.

Sander Pham
Thanks, I'm still new to C# and WPF, so would I create a new class that implements the INotifyPropertyChanged? How would I use that to connect the listbox to the database?
Ashley
Regarding your question about connecting the listbox to the database;You can have in the same data class your create/update/delete/list methods which will interact with your database and update properties in your data class (causing OnPropertyChanged to fire).
Sander Pham
+1  A: 

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.

Sander Pham
A: 

Thanks for the help Sander. Here is my sample class that implements INotifyPropertyChanged:

public class ADDRESS : INotifyPropertyChanged
    {
        private string address;
      public event PropertyChangedEventHandler PropertyChanged;

      public ADDRESS()
      {
      }

      public ADDRESS(string value)
      {
          this.address = value;
      }

      public string ADDRESS
      {
          get { return address; }
          set
          {
              address = value;
              OnPropertyChanged("ADDRESS");
          }
      }

      protected void OnPropertyChanged(string address)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(address));
          }
      }
    }

Is this on the right track? Btw, in my initial listing item1 is actually ADDRESS but I was just trying to simplify for this post. Does that mean I need a constructor for each item in my listbox, so 5 in total? Or do I need a new class for each item with exactly the same names as the binding source for each listbox item, eg Class item2, Class item3 etc? Thanks again!

Ashley