2017-06-19 69 views
0

我已經做了大量的閱讀,但我顯然仍然失去了一些東西。我可以通過將ObservableCollection傳遞給類構造函數來解決它,但我想了解它爲什麼失敗。輸出窗口中沒有任何內容表明綁定已損壞。一個可見性轉換器我對ObservableCollection的屬性工作正常。這是XAML。問題是與DetailsListBoxListView不更新時ItemsSource更改

<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> 

這裏是代碼隱藏創建類的實例和設置的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; 

下面是一些類定義的,包括的PropertyChanged處理程序。 OnPropertyChanged正在被調用。我原本擁有DetailsList作爲列表<>,但讀它應該是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)); 
    } 

最後,VisiblityConverter工作,並且也綁定到DetailsList。當它爲空時,我想隱藏它。我認爲它可能是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(); 
    } 
} 

這裏是的TraceLevel設置爲高後的輸出窗口:

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') 
+0

你確定內存中是否有*項目?如在; 'hostports'不是一個空集合? PropertyChanged上的'= delegate'真的很奇怪 – BradleyDotNET

+0

ListBox是你引用的ListView嗎?什麼是'NodeAsThumb'? –

+0

@BradleyDotNET hostports不爲空。我用它來做其他事情。 –

回答

1

我不會綁定一個ObservableCollection和重新初始化。即使有INotifyPropertyChangedOnPropertyChanged

請只是Clear()ObservableCollection和使用PropertyCount申請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"); 
    } 


} 

您將需要另一個IValueConverter,但你有這麼多的可能性。像IsEmpty,Count,HasItems。我想你應該已經明白了。

+2

我喜歡這個新的C#語法的只讀屬性,它*真正*可以幫助您不小心更換集合引用。 'public System.Collections.ObjectModel.ObservableCollection MyCollection {get;}'。現在,只有*的地方,實際的集合引用可以設置一次,在構造函數中。它強制你以正確的方式處理修改集合。 –

+2

'私人設置'仍然允許參考從班內換出。您可以使用該語法而不使用'private set'來獲得相同的效果(在最新的c#版本中),並且不允許交換新實例的安全性。 –

+0

我更新了它謝謝 – Peter