[英]ListView Not Updating When ItemsSource Changed
I've done a lot of reading on this, but I'm evidently still missing something. 我已经做了很多阅读,但是显然我仍然缺少一些东西。 I could work around it by passing in an ObservableCollection to the class constructor but I want to understand why it's failing.
我可以通过将ObservableCollection传递给类构造函数来解决此问题,但我想了解为什么它会失败。 There is nothing in the output window indicating a broken binding.
输出窗口中没有任何内容表明绑定已损坏。 A Visibility Converter I have on the ObservableCollection<> property works fine.
我在ObservableCollection <>属性上具有的可见性转换器工作正常。 Here is the XAML.
这是XAML。 the problem is with the DetailsListBox
问题出在DetailsListBox
<StackPanel Grid.Column="2" >
<Label
Name="DetailsLabel" Foreground="Black" Height="20" Width="Auto"
FontSize="8"
Content="{Binding Description, Converter={StaticResource DetailConverter}}"
Visibility="{Binding Path=DetailsList, Converter={StaticResource DetailsVisConverter}}"
Background="Transparent"
Margin="0" Padding="0"
HorizontalAlignment="Center">
</Label>
<ListBox
Name="DetailsList"
ItemsSource="{Binding Path=DetailsList}"
Visibility="{Binding Path=DetailsList, Converter={StaticResource DetailsVisConverter}}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=.}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
Here is the code-behind creating the class instance and setting up the ObservableCollection: 这是创建类实例并设置ObservableCollection的背后代码:
NodeAsThumb hosthumb = CreateClusterNodeAsThumb(host);
hosthumb.DataContext = hosthumb;
ObservableCollection<string> PortDetails = new ObservableCollection<string>();
foreach (PortResult pr in hostports) { PortDetails.Add(pr.Interface); }
hosthumb.DetailsList = PortDetails;
Here is some of the class definition, including the PropertyChanged handler. 这是一些类定义,包括PropertyChanged处理程序。 OnPropertyChanged is getting called.
OnPropertyChanged被调用。 I originally had DetailsList as a List<>, but read it should be an ObservableCollection<> as it would fire the ICollectionChanged, but that doesn't appear to be happening.
我最初有DetailsList作为List <>,但是读取它应该是ObservableCollection <>,因为它将触发ICollectionChanged,但似乎没有发生。
public class NodeAsThumb :FabricThumb, INotifyPropertyChanged
{
private ObservableCollection<string> _detailsList = null;
public ObservableCollection<string> DetailsList { get { return _detailsList; } set { _detailsList = value; OnPropertychanged("DetailsList"); } }
// I have verified that this gets fired when DetailsList is assigned to
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
private void OnPropertychanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
Lastly, the VisiblityConverter that does work, and is also bound to DetailsList. 最后,有效的VisiblityConverter并绑定到DetailsList。 When it is null, I want it hidden.
当它为null时,我希望将其隐藏。 I thought it might be the VisibilityConverter still having it collapsed, so I disabled it but ended up with an always empty list.
我以为可能是VisibilityConverter仍处于折叠状态,所以我禁用了它,但最终得到一个始终为空的列表。
public class DetailsListVisiblityConverter : IValueConverter
{
public Object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return Visibility.Collapsed;
}
else
{
return Visibility.Visible;
}
}
public Object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Here is the output window after setting the TraceLevel to High: 将TraceLevel设置为High之后,这是输出窗口:
System.Windows.Data Warning: 56 : Created BindingExpression (hash=1635228) for Binding (hash=30787047)
System.Windows.Data Warning: 58 : Path: 'DetailsList'
System.Windows.Data Warning: 60 : BindingExpression (hash=1635228): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=1635228): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=1635228): Attach to System.Windows.Controls.ListBox.ItemsSource (hash=59834015)
System.Windows.Data Warning: 67 : BindingExpression (hash=1635228): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=1635228): Found data context element: ListBox (hash=59834015) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=1635228): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=1635228): Resolve source deferred
System.Windows.Data Warning: 56 : Created BindingExpression (hash=61434729) for Binding (hash=30787047)
System.Windows.Data Warning: 58 : Path: 'DetailsList'
System.Windows.Data Warning: 60 : BindingExpression (hash=61434729): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=61434729): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=61434729): Attach to System.Windows.Controls.ListBox.ItemsSource (hash=51565323)
System.Windows.Data Warning: 67 : BindingExpression (hash=61434729): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=61434729): Found data context element: ListBox (hash=51565323) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=61434729): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=61434729): Resolve source deferred
System.Windows.Data Warning: 56 : Created BindingExpression (hash=27129916) for Binding (hash=30787047)
System.Windows.Data Warning: 58 : Path: 'DetailsList'
System.Windows.Data Warning: 60 : BindingExpression (hash=27129916): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=27129916): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=27129916): Attach to System.Windows.Controls.ListBox.ItemsSource (hash=40297137)
System.Windows.Data Warning: 67 : BindingExpression (hash=27129916): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=27129916): Found data context element: ListBox (hash=40297137) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=27129916): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=27129916): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=1635228): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=1635228): Found data context element: ListBox (hash=59834015) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=1635228): Activate with root item NodeAsThumb (hash=46479497)
System.Windows.Data Warning: 107 : BindingExpression (hash=1635228): At level 0 using cached accessor for NodeAsThumb.DetailsList: RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 104 : BindingExpression (hash=1635228): Replace item at level 0 with NodeAsThumb (hash=46479497), using accessor RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 101 : BindingExpression (hash=1635228): GetValue at level 0 from NodeAsThumb (hash=46479497) using RuntimePropertyInfo(DetailsList): <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=1635228): TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=1635228): TransferValue - using final value <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=61434729): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=61434729): Found data context element: ListBox (hash=51565323) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=61434729): Activate with root item NodeAsThumb (hash=1807185)
System.Windows.Data Warning: 107 : BindingExpression (hash=61434729): At level 0 using cached accessor for NodeAsThumb.DetailsList: RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 104 : BindingExpression (hash=61434729): Replace item at level 0 with NodeAsThumb (hash=1807185), using accessor RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 101 : BindingExpression (hash=61434729): GetValue at level 0 from NodeAsThumb (hash=1807185) using RuntimePropertyInfo(DetailsList): <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=61434729): TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=61434729): TransferValue - using final value <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=27129916): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=27129916): Found data context element: ListBox (hash=40297137) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=27129916): Activate with root item NodeAsThumb (hash=22613965)
System.Windows.Data Warning: 107 : BindingExpression (hash=27129916): At level 0 using cached accessor for NodeAsThumb.DetailsList: RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 104 : BindingExpression (hash=27129916): Replace item at level 0 with NodeAsThumb (hash=22613965), using accessor RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 101 : BindingExpression (hash=27129916): GetValue at level 0 from NodeAsThumb (hash=22613965) using RuntimePropertyInfo(DetailsList): <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=27129916): TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=27129916): TransferValue - using final value <null>
'FabricAnalyzer.exe' (CLR v4.0.30319: FabricAnalyzer.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemCore\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemCore.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
System.Windows.Data Warning: 56 : Created BindingExpression (hash=63575268) for Binding (hash=22674843)
System.Windows.Data Warning: 58 : Path: 'DetailsList'
System.Windows.Data Warning: 60 : BindingExpression (hash=63575268): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=63575268): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=63575268): Attach to System.Windows.Controls.ListBox.ItemsSource (hash=66716242)
System.Windows.Data Warning: 67 : BindingExpression (hash=63575268): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=63575268): Found data context element: ListBox (hash=66716242) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=63575268): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=63575268): Resolve source deferred
System.Windows.Data Warning: 56 : Created BindingExpression (hash=55244468) for Binding (hash=22674843)
System.Windows.Data Warning: 58 : Path: 'DetailsList'
System.Windows.Data Warning: 60 : BindingExpression (hash=55244468): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=55244468): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=55244468): Attach to System.Windows.Controls.ListBox.ItemsSource (hash=28507895)
System.Windows.Data Warning: 67 : BindingExpression (hash=55244468): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=55244468): Found data context element: ListBox (hash=28507895) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=55244468): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=55244468): Resolve source deferred
System.Windows.Data Warning: 56 : Created BindingExpression (hash=53268321) for Binding (hash=22674843)
System.Windows.Data Warning: 58 : Path: 'DetailsList'
System.Windows.Data Warning: 60 : BindingExpression (hash=53268321): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=53268321): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=53268321): Attach to System.Windows.Controls.ListBox.ItemsSource (hash=20831783)
System.Windows.Data Warning: 67 : BindingExpression (hash=53268321): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=53268321): Found data context element: ListBox (hash=20831783) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=53268321): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=53268321): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=63575268): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=63575268): Found data context element: ListBox (hash=66716242) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=63575268): Activate with root item NodeAsThumb (hash=19140135)
System.Windows.Data Warning: 107 : BindingExpression (hash=63575268): At level 0 using cached accessor for NodeAsThumb.DetailsList: RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 104 : BindingExpression (hash=63575268): Replace item at level 0 with NodeAsThumb (hash=19140135), using accessor RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 101 : BindingExpression (hash=63575268): GetValue at level 0 from NodeAsThumb (hash=19140135) using RuntimePropertyInfo(DetailsList): <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=63575268): TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=63575268): TransferValue - using final value <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=55244468): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=55244468): Found data context element: ListBox (hash=28507895) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=55244468): Activate with root item NodeAsThumb (hash=31430389)
System.Windows.Data Warning: 107 : BindingExpression (hash=55244468): At level 0 using cached accessor for NodeAsThumb.DetailsList: RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 104 : BindingExpression (hash=55244468): Replace item at level 0 with NodeAsThumb (hash=31430389), using accessor RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 101 : BindingExpression (hash=55244468): GetValue at level 0 from NodeAsThumb (hash=31430389) using RuntimePropertyInfo(DetailsList): <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=55244468): TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=55244468): TransferValue - using final value <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=53268321): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=53268321): Found data context element: ListBox (hash=20831783) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=53268321): Activate with root item NodeAsThumb (hash=35867003)
System.Windows.Data Warning: 107 : BindingExpression (hash=53268321): At level 0 using cached accessor for NodeAsThumb.DetailsList: RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 104 : BindingExpression (hash=53268321): Replace item at level 0 with NodeAsThumb (hash=35867003), using accessor RuntimePropertyInfo(DetailsList)
System.Windows.Data Warning: 101 : BindingExpression (hash=53268321): GetValue at level 0 from NodeAsThumb (hash=35867003) using RuntimePropertyInfo(DetailsList): <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=53268321): TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=53268321): TransferValue - using final value <null>
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=WWPN; DataItem=null; target element is 'DataGridTextColumn' (HashCode=30423852); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=SwitchName; DataItem=null; target element is 'DataGridTextColumn' (HashCode=33040847); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=WWPN; DataItem=null; target element is 'DataGridTextColumn' (HashCode=19299281); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=SwitchName; DataItem=null; target element is 'DataGridTextColumn' (HashCode=47325326); target property is 'Visibility' (type 'Visibility')
I would not bind an ObservableCollection
and reinitialize it. 我不会绑定
ObservableCollection
并重新初始化它。 Even with INotifyPropertyChanged
and OnPropertyChanged
. 即使使用
INotifyPropertyChanged
和OnPropertyChanged
。
Please just Clear()
your ObservableCollection
and use a Property
like Count
to apply Visibility
. 请只
Clear()
您的ObservableCollection
并使用Count
类的Property
应用Visibility
。
public class MyClass : BaseViewModel
{
public System.Collections.ObjectModel.ObservableCollection<string> MyCollection
{
get;
}
public MyClass()
{
MyCollection = new System.Collections.ObjectModel.ObservableCollection<string>();
MyCollection.CollectionChanged += MyCollection_CollectionChanged;
}
public bool IsEmpty
{
get
{
return MyCollection.Count == 0;
}
}
public void RefillMyCollection(IEnumerable<string> values)
{
MyCollection.Clear();
foreach(string value in values)
{
MyCollection.Add(value);
}
}
private void MyCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged("IsEmpty");
}
}
You will need another IValueConverter
, but u have so many possibilitys. 您将需要另一个
IValueConverter
,但是您有很多可能性。 Like IsEmpty
, Count
, HasItems
. 像
IsEmpty
, Count
, HasItems
。 I think you get the idea. 我想你应该已经明白了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.