Wpf Listbox itemssource不随源更新
我的XAML中有两个列表框,它们都是多选的。我已使用同步列表框的selectedItems 需要根据lbOrg中选择的值填充lbSite。因此,为了让ViewModel知道为lbOrg更改的选择,我正在为lbOrg的Selected Items列表处理selection changed事件,并调用一个方法来填充lbSite的值 我将这些属性定义为:Wpf Listbox itemssource不随源更新,wpf,c#-4.0,binding,mvvm,listbox,Wpf,C# 4.0,Binding,Mvvm,Listbox,我的XAML中有两个列表框,它们都是多选的。我已使用同步列表框的selectedItems 需要根据lbOrg中选择的值填充lbSite。因此,为了让ViewModel知道为lbOrg更改的选择,我正在为lbOrg的Selected Items列表处理selection changed事件,并调用一个方法来填充lbSite的值 我将这些属性定义为: public ObservableCollection<object> SelectedOrg { get
public ObservableCollection<object> SelectedOrg
{
get
{
return _selectedOrg;
}
set
{
_selectedOrg = value;
OnPropertyChanged("SelectedOrg");
}
}
public ObservableCollection<object> AvailableSites
{
get
{
return _availableSites;
}
set
{
_availableSites = value;
OnPropertyChanged("AvailableSite");
}
}
我的ViewModelBase实现了INotifyPropertyChanged。此外,属性被定义为ObservaleCollection,其中对象已从两个属性中的单独自定义类中强制转换
我想这应该很容易,但是由于一些奇怪的原因,listbox没有收到有关属性更改的通知,因此对源的更改没有反映在视图上。我已经将eventHandler与代码隐藏中的lbOrg的Selection changed事件绑定,以检查lbSite的Itemssource是否得到更新,但它没有检查AvailableSites属性是否具有所需的值
非常感谢您的帮助。您错过了一个s:
OnPropertyChanged("AvailableSite");
应该是
OnPropertyChanged("AvailableSites");
我的ViewModelBase中有一些代码在调试模式下检查字符串,这对捕获此类错误非常有帮助:
[Conditional("DEBUG")]
private void CheckPropertyNameIsValid(string propertyName)
{
// INotifyPropertyChanged notifies via strings, so use
// this helper to check that the string is accurate in debug builds.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
throw new Exception("Invalid property name: " + propertyName);
}
还有各种优雅的解决方案,可以完全消除对INotifyPropertyChanged的魔法字符串的依赖,但我还没来得及使用这些解决方案。太棒了。。。对不起,浪费了你的时间,我在想,因为我把其他事情都做对了。哇,我从没见过。。。谢谢你一吨…:别担心,相信我,我去过那里。
OnPropertyChanged("AvailableSites");
[Conditional("DEBUG")]
private void CheckPropertyNameIsValid(string propertyName)
{
// INotifyPropertyChanged notifies via strings, so use
// this helper to check that the string is accurate in debug builds.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
throw new Exception("Invalid property name: " + propertyName);
}