如何在SilverLight 3项目中刷新ListBox.ItemsSource? [英] How to refresh ListBox.ItemsSource in a SilverLight 3 project?
问题描述
我的XAML,我定义了一个ListBox
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>
在后面的代码中,我定义了模块级的ObservableCollection设备.
In the code-behind, I define a module-level ObservableCollection devices.
private ObservableCollection<Device> devices = new ObservableCollection<Device>();
在OnNavigatedTo事件中,我将集合绑定到列表框
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.
我想念什么?
推荐答案
听起来像Device
对象中缺少INotifyPropertyChanged
实现.
Sounds like you are missing INotifyPropertyChanged
implementation in the Device
object.
例如:-
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在属性更改时得到通知.
This will enable the TextBlocks bound to specific properties to be notified when the properties change.
这篇关于如何在SilverLight 3项目中刷新ListBox.ItemsSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!