views:

1079

answers:

1

I my XAML, I have a ListBox defined

<ListBox x:Name="lstStatus" Height="500" 
         Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" VerticalAlignment="Top" Margin="2, 2, 2, 2">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image />
                <TextBlock Width="70" Text="{Binding FriendlyID}" />
                <TextBlock Width="150" Text="{Binding StatusName}" />
                <TextBlock Width="70" Text="{Binding ANI}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Effect>
        <DropShadowEffect/>
    </ListBox.Effect>
</ListBox>

In the code-behind, I define a module-level ObservableCollection devices.

private ObservableCollection<Device> devices = new ObservableCollection<Device>();

In the OnNavigatedTo event I bind the collection to the listbox

lstStatus.ItemsSource = devices;

The collection will most likely not grow or shrink but the objects within themselves change all the time. For whatever reason, the list does not get updated, when I execute the following code:

Device selectedDevice = null;
foreach (Device dv in devices)
{
    if (dv.IsTrunk)
    {
        selectedDevice = dv;
        break;
    }
}

if (selectedDevice != null)
    selectedDevice.StatusName = DateTime.Now.ToString();
else
    throw new Exception();

In fact, the only way I was able to halfway get it to work is to fake it out, remove items from the list and then add it back up. Obviously not a solution for the long term.

What am I missing?

+1  A: 

Sounds like you are missing INotifyPropertyChanged implementation in the Device object.

For example:-

 public class Device : INotifyPropertyChanged
 {
     private string _StatusName;
     public string StatusName
     {
         get { return _StatusName; }
         set
         {
             _StatusName = value;
             NotifyPropertyChanged("StatusName");
         }

     }

     private void NotifyPropertyChanged(string name)
     {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(name));
     }

     #region INotifyPropertyChanged Members

     public event PropertyChangedEventHandler PropertyChanged;

     #endregion       
 }

This will enable the TextBlocks bound to specific properties to be notified when the properties change.

AnthonyWJones