在我的 windows phone 8 应用程序中,我创建了真正可观察的集合,以便在集合中的项目更改时注意。这是收集代码:
public class TrulyObservableCollection<T> : ObservableCollection<T> where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
: base()
{
CollectionChanged += new NotifyCollectionChangedEventHandler(TrulyObservableCollection_CollectionChanged);
}
void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Object item in e.NewItems)
{
var test = item as INotifyPropertyChanged;
(item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
(item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
OnCollectionChanged(a);
}
}
我要描述的情况发生在两个控件上:扩展器视图(silverlight 工具包)和扩展器控件(telerik 控件)。在我看来,我有扩展器视图/控件的列表。在项目点击事件中,我想更改点击项目和刷新列表中的某些内容。这是我如何做的代码:
视图模型:
public TrulyObservableCollection<SymbolRecord> Symbols {get; set;}
private RelayCommand<SymbolRecord> tapCommand;
public RelayCommand<SymbolRecord> TapCommand
{
get
{
return tapCommand ?? (tapCommand = new RelayCommand<SymbolRecord>((item) =>
{
item.Symbol = "test";
}));
}
}
看法:
<telerikData:RadJumpList x:Name="ListControl" Grid.Row="1"IsCheckModeEnabled="False" GroupPickerItemTemplate="{StaticResource JumpListHeaderItemTemplate}" GroupHeaderTemplate="{StaticResource JumpListHeaderTemplate}" ItemsSource="{Binding Path=Symbols}" >
<telerikData:RadJumpList.ItemTemplate>
<DataTemplate>
<StackPanel>
<toolkit:ExpanderView ItemsSource="{Binding}" Expander="{Binding}" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" ExpanderTemplate="{StaticResource ExpanderControlContentTemplate}" >
<toolkit:ExpanderView.Items> <TextBox Text="TEST"></TextBox></toolkit:ExpanderView.Items>
</toolkit:ExpanderView>
</StackPanel>
</DataTemplate>
</telerikData:RadJumpList.ItemTemplate>
</telerikData:RadJumpList>
<i:Interaction.Triggers>
<i:EventTrigger EventName="ItemTap" SourceName="ListControl" >
<cmd:EventToCommand Command="{Binding TapCommand}" CommandParameter="{Binding SelectedItem,
ElementName=ListControl}" />
</i:EventTrigger>
</i:Interaction.Triggers>
当我在模拟器中单击项目时,属性 Symbol 正在发生变化,但更多的随机项目正在扩展。不知道为什么会这样。请帮我