none
Many To Many update binding source problem LINQ & WPF RRS feed

  • Question

  • Hi.
    I have a mandatory many to many relationship. This means everytime I create Object A I need to create N relations with the N Mandatory Properties. Example; Object A={1=Car, 2=Truck} , Properties={1=color, 2=model}... If I create a new object A called bike, I need to add two relations, the color and model. These are made in 3 tables. ObjectsA(ObjectID, Name), ObjectsProperties(ObjectAID,PropertieID,Value) and Properties(PropertieID,Name,defaultValue).
    In WPF I have a listview with al the A objects, and a grid with the properties, there is a row for every property for a particular object.
    When the windows is loaded i filled the following collections with data...:
     
           private IEnumerable<ObjectsA> ObjectsAEnum;
            private CollectionViewSource ObjectsASource;
            private BindingListCollectionView ObjectsAData;
            private CollectionViewSource ObjectsPropertiesSource;
            private BindingListCollectionView ObjectsPropertiesData;

    I use binding to map the collections:
    <CollectionViewSource x:Key="ObjectsAData" />
    <CollectionViewSource Source="{Binding Source={StaticResource ObjectsAData}, Path='ObjectsAProperties'}" x:Key="PropertiesData" />
    ...
    I used a listview to show a list of ObjectsA, and the grid is something like this...

                <ListView Width="Auto" IsSynchronizedWithCurrentItem="True"   ItemsSource="{Binding Source={StaticResource PropertiesData}}">
                        <ListView.View>
                                    <GridView>
                                        <GridViewColumn Header="Property">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <ContentControl
                                                        Content="{Binding Path=Properties.Name}">
                                                    </ContentControl>
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                        <GridViewColumn Header="Value">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBox Text="{Binding Path=Value}" Width="80"/>
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                    </GridView>
                        </ListView.View>
           </ListView>



    I create new ObjectA by clicking a button, the process creates all relations needed in code using linq like this:

                ObjectA o = new ObjectA();
                o.Name = "<Name>";
                db.ObjectsA.InsertOnSubmit(o);
                db.SubmitChanges();
                //getting the list of properties
                var plist = db.Properties;
                foreach (Properties p in plist)
                {
                    ObjectsProperties op = new ObjectsProperties();
                    op.Puesto_id = o.ObjectsAID;
                    op.Pep_id = p.PropertiesID;
                    op.Value = p.Defaultvalue;
                    db.PuestosProperties.InsertOnSubmit(ppp);
                }
                db.SubmitChanges();


    At this point the new objectA and every property were inserted in the Database.
    But the grid isn't updated.... if I close the windows and open it again the data is ok, but the update isn't dynamic.
    What should I use to update the form??????
    Any reference to see how to implement this thing????

    Thanks in advance...
    Monday, November 9, 2009 10:54 PM

Answers

  • pamorale,
    Two thing I would do:
    1. Make sure your collection is an ObservableCollection - it signals to UI if the collection is changed in anyway to refresh it and show it.
    2.Make sure your items class implements INotifyPropertyChanged - This also signals the UI when Properties are set and refreshes the view.

    Otherwise you seem to be doing fine, so hope this will help you.
    noorbakhsh
    • Marked as answer by Linda Liu Monday, November 16, 2009 5:51 AM
    Tuesday, November 10, 2009 12:17 AM

All replies

  • pamorale,
    Two thing I would do:
    1. Make sure your collection is an ObservableCollection - it signals to UI if the collection is changed in anyway to refresh it and show it.
    2.Make sure your items class implements INotifyPropertyChanged - This also signals the UI when Properties are set and refreshes the view.

    Otherwise you seem to be doing fine, so hope this will help you.
    noorbakhsh
    • Marked as answer by Linda Liu Monday, November 16, 2009 5:51 AM
    Tuesday, November 10, 2009 12:17 AM
  • Thank you Noorbakhsh
    I'll check that, I was reading about it and ObservableCollection and INotifyPropertyChanged were mentioned...

    cheers
    Tuesday, November 10, 2009 1:58 PM