我在刷新listview数据时遇到问题。除非我致电:
,否则不会更新List.ItemsSource = null;
List.ItemsSource = listviewSource;
这是有效但看起来不太好(屏幕闪烁)所以我想按照人们的建议使用INotifyPropertyChanged。
我的listviewSource是ObservableCollection<等级2> Class2实现Class1,Class1实现INotifyPropertyChanged。
public class Class1 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
...
}
public class Class2 : Class1
{
private string ImgSource;
public string imgSource
{
get { return this.ImgSource; }
set
{
this.ImgSource = value;
NotifyPropertyChanged("imgSource");
}
}
....
}
imgSource绑定到ListView但是当它发生变化时没有任何反应......我做错了什么?
XAML标记
<ListView x:Name="List" Width="400" HorizontalAlignment="Center" Loaded="List_Loaded">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Top"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Tag="{Binding number}" Background="#FF0E1D23" Margin="0,5" >
<Image Margin="339,10,23,13" Source="{Binding imgSource}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
答案 0 :(得分:1)