views:

259

answers:

3

I have set up a grid and bound it to a collection. I can edit the items in the collection through my grid and the changes get propagated to the collection. And, the GUI is showing everything in the collection at the time the ItemSource is set. But, I am programmatically changing some of the items in the collection (after the ItemSource is set) and these changes aren't reflected in the grid/GUI. Is there something else I need to do in order to get it to refresh. FYI, for the fields I want to edit (MoveToResource, ResourceKey, and Resource Type), I have set the mode to TwoWay. Below is my grid.

            <ListView Name="lstXAMLStrings" Margin="5"  Grid.Row="1">
            <ListView.View>                    
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Extract?">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox Content="" IsChecked="{Binding Path=MoveToResource, Mode=TwoWay}"  ></CheckBox>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Text">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <local:RichTextBlock RichText="{Binding Path=FormattedMatchedLines}"  TextWrapping="Wrap"  Width="650"></local:RichTextBlock>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Key Name">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Path=ResourceKey, Mode=TwoWay}" Width="150"></TextBox>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Resource Type">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding Source={StaticResource odp}}" SelectedItem="{Binding Path=Resource, Mode=TwoWay}"></ComboBox>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView.Columns>
                </GridView>                   
            </ListView.View>
        </ListView>
+2  A: 

Does your [view]model class implement INotifyPropertyChanged and fire the event whenever the property set accessor is used?

Drew Marsh
+1  A: 

You need to make sure that your collection items implement INotifyPropertyChanged.

If each item you're changing programatically (correctly) implements that, your ListView/GridView will stay current.

This will work if you modify your collection items programatically, or in another screen.

Reed Copsey
That is what I'm missing. Thanks.
bsh152s
Ok, now that I implement INotifyPropertyChanged, the items are updated when changed programmatically. But now changes made through the GUI don't propagate to the collection items. Is there any way to do both?
bsh152s
Brandon: It should work both ways, provided everything is implemented properly. Can you show your collection implementation?
Reed Copsey
Brandon: The INotifyPropertyChanged implementation shouldn't change the binding into the collection at all - that should be exactly the same.
Reed Copsey
@Brandon, you should use TwoWay mode of binding inside your binding tag.
Akash Kava
+2  A: 

You need to make sure that the collection itself that you're databinding to is an observable collection (a class that implements the INotifyCollectionChanged interface). You might be able to alternatively roll your own class that implements INotifyCollectionChanged, but that's the only reason ObservableCollection exists so it could save you some time.

There's an msdn article on how to do it.

Joseph
Joseph: This will only make sure that adding/removing elements from the collection is updating the view. It will have no effect on changing a property within one element of the collection. The collection's contained class needs to implement INotifyPropertyChanged for that to work...
Reed Copsey
@Reed Thanks, I misread the question I thought he was adding/removing items programmatically from his collection.
Joseph