0

I have ListView in my View whose itemsSource is bound to an Observable collection. this is my view

<ListView x:Name="Details" 
         ItemsSource="{Binding Details}"
         Grid.Row="1"
         Grid.Column="1"
         Grid.ColumnSpan="3">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Background="White" Width="350">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="175"/>
                    <ColumnDefinition Width="175"/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Row="0"
                           Grid.Column="0"
                           Text="{Binding Key}"/>

                <TextBox Grid.Row="0"
                           Grid.Column="1"
                           Text="{Binding Value}"/>

            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Question: when i run my app and change my TextBox values, how do i get a new collection in my viewModel which has those changes ONLY? I want a collection of values i have changed Only. Currently i get a collection of values i have changed and old values . Here is my MainViewModel:

private ObservableCollection<Details> _details;
public ObservableCollection<Details> Details
    {
        get { return _details; }
        set
        {
            _details= value;
            RaisePropertyChanged();
        }
    }

 public MainViewModel()
{
    Details= new ObservableCollection<Details>
    {
        new Details() {Key = "age", Value = 25},
        new Details() {Key = "sex", Value = "female"},
        new Details() {Key = "height", Value = 3}
    };
}

Details class:

public class Details
{
public string Key { get; set; }
public object Value { get; set;}}

In the above code if i change age to 20 ,,, i will get a collection of :(age 20, sex female,height 3) yet i only want (age 20) ,ie the Details object of the textBox i changed ONLY!!. I dont want to compare original collection and new collection to get that. Is there any way i can fire an event or something a long those lines.

Thanks

Tee
  • 39
  • 1
  • 9
  • It is completely VM logic. You should subscribe to each collection value's PropertyChanged event and in the handler put the item which is the source of event into a separate collection. – galenus Nov 05 '14 at 14:18

1 Answers1

0

You will want to wire up an event handler for the ObservableCollection CollectionChanged event. The handler will then have this passed into it a EventArguments: http://msdn.microsoft.com/en-us/library/system.collections.specialized.notifycollectionchangedeventargs(v=vs.110).aspx

Notice that you will be able to see newitems per your request.

This post shows a very clear example of how to do this...

Notify ObservableCollection when Item changes

Now if you want to dive in deeper, and get just one of the property values that changed you will need to implement your own notification system within the Details class. There are many ways to do this, one is the infamous DepedencyProperty itself which has many hooks for changes. But you could simply implement EventHanlder or the generic EventHandler as a static varible in class defintion. This allows other classes to "wire-up" to the event.

Community
  • 1
  • 1
JWP
  • 6,672
  • 3
  • 50
  • 74