使用绑定到 属性 的图像源刷新 ListView 项目
Refresh ListView item with Image's Source bound to property
我在刷新列表视图数据时遇到问题。它不会更新,除非我调用:
List.ItemsSource = null;
List.ItemsSource = listviewSource;
这是可行的,但看起来不太好(屏幕闪烁)所以我想按照人们的建议使用 INotifyPropertyChanged。
我的 listviewSource 是 ObservableCollection,其中 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>
似乎 Image
的 Source
默认 binding mode 是 OneTime
。
将其明确更改为 OneWay
:
Source="{Binding imgSource, Mode = OneWay}"
我在刷新列表视图数据时遇到问题。它不会更新,除非我调用:
List.ItemsSource = null;
List.ItemsSource = listviewSource;
这是可行的,但看起来不太好(屏幕闪烁)所以我想按照人们的建议使用 INotifyPropertyChanged。
我的 listviewSource 是 ObservableCollection
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>
似乎 Image
的 Source
默认 binding mode 是 OneTime
。
将其明确更改为 OneWay
:
Source="{Binding imgSource, Mode = OneWay}"