【发布时间】:2016-12-19 04:47:50
【问题描述】:
我有一个 Observable 集合,但是在更新集合之后,即使在引发 Property Changed 事件后,我的 Listview 也没有更新,请参见下面的代码:-
看下面的 XAML:-
<ListView Grid.Row="1" Grid.Column="0" Name="lvGroups" Margin="0,34,0,0"
Grid.RowSpan="2" ItemsSource="{Binding Path=*VideoGroupList*,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<GridView>
<GridViewColumn Width="150" DisplayMemberBinding="{Binding *Name*}" />
</GridView>
看下面的类
public class VideoGroupViewModel : ObservableEnitiy
{
public ObservableCollection<Group> VideoGroupList { get; set; }
}
public abstract class ObservableEnitiy : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
this.VerifyPropertyName(propertyName);
if (this.PropertyChanged != null)
{
var e = new PropertyChangedEventArgs(propertyName);
this.PropertyChanged(this, e);
}
}
}
[Serializable]
public class Group : PropertyChangedNotification
{
public int ID { get; set; }
[Required(ErrorMessage = "Group name is required.")]
public string *Name*
{
get { return GetValue(() => Name); ; }
set
{
SetValue(() => Name, value);
}
}
[XmlIgnore]
public bool IsSelected { get; set; }
}
protected T GetValue<T>(Expression<Func<T>> propertySelector)
{
string propertyName = GetPropertyName(propertySelector);
return GetValue<T>(propertyName);
}
我这样叫
VideoGroupList = new ObservableCollection<Group>(videoGroupManager.GetVideoGroups());
OnPropertyChanged("VideoGroupList");
【问题讨论】:
-
这个问题通常是由于控件刷新事件没有识别出控件已经改变引起的。通常有效的技巧是在更新列表之前添加 VideoGroupList = null。
-
请注意,在 ItemsSource Binding 上设置
Mode=TwoWay和UpdateSourceTrigger=PropertyChanged是多余的。它没有任何效果,因为没有从绑定的目标到源属性的数据流。 -
@jdweng 我在更新列表之前尝试了 VideoGroupList = null 但不起作用。 :(
-
您确定您正在获取代码吗?在代码上设置断点。
-
@jdweng 是的,我尝试过,但没有运气