How to update ListBox.ItemsSource in a SilverLight 3 project?

I am 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 code lag, I define ObservableCollection devices at the module level.

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

In the OnNavigatedTo event, I bind the collection to a list

lstStatus.ItemsSource = devices;

The collection, most likely, will not grow or shrink, but the objects within themselves change all the time. For some reason, the list is not 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 halfway through getting it to work was to fake it, remove items from the list, and then add it back. Obviously, this is not a solution in the long run.

What am I missing?

+3
1

, INotifyPropertyChanged Device.

: -

 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       
 }

TextBlocks .

+4

Source: https://habr.com/ru/post/1732383/


All Articles