views:

298

answers:

1

Hi,

I am binding to the values of a hash table from within a datatemplate and the values display fine, but the changes are not persisted to the object when I make changes in a text box for example. Any idea why this is?

   <DataTemplate x:Key="ResponseItemTemplate">
        <StackPanel Orientation="Horizontal" >
            <TextBox Width="200" Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>
    </DataTemplate>

Thanks for your time

A: 

This will not work because you are using a Hashtable, which does not support INotifyPropertyChanged. Instead of binding to a hash table, you should bind to an ObservableCollection, which will handle all of the change notification for you. If hashtable-like behavior is a must, you can simply extend ObservableCollection and override the InsertItem and SetItem methods. Here is an example of that:

public class SetCollection<T> : ObservableCollection<T> 
{
   protected override void InsertItem(int index, T item)
   {
      if (Contains(item))
         throw new ItemExistsException(item);

      base.InsertItem(index, item);
   }

   protected override void SetItem(int index, T item)
   {
      int i = IndexOf(item);
      if (i >= 0 && i != index)
         throw new ItemExistsException(item);

      base.SetItem(index, item);
   }
}
Charlie