2015-10-16 70 views
0

編輯:改變清單的ObservableCollection,得到了相同的ISSUË添加到一個的ObservableCollection <string>

我有持有所需的應用程序運行時的持續時間值的全局對象。出於某種原因,ListView沒有被填充,不太確定我是否編寫了錯誤的代碼,或者ListView是否由於某種原因而沒有更新。

這裏是類:

App.xaml.cs

/// <summary> 
/// Global values for use during application runtime 
/// </summary> 
public class runtimeObject : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string name) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
     } 

    } 
    private ObservableCollection<string> _hashList; 
    public ObservableCollection<string> hashList 
    { 
     get { return _hashList; } 
     set 
     { 
      if (value = "true") 
      { 
       _hashList.Clear(); 
      } 
      else 
      { 
       _hashList.Add(value); 
       OnPropertyChanged("hashList"); 
      } 
     } 
    } 
} 

我創建了一個命令來填充這個名單,所以我可以測試它的約束力。下面是一個命令:

Commands.cs

/// <summary> 
/// Command: Test 
/// </summary> 
public static RoutedUICommand Test 
{ 
    get { return _Test; } 
} 
public static void Test_Executed(object sender, 
      ExecutedRoutedEventArgs e) 
{ 
    var runtime = (runtimeObject)Application.Current.TryFindResource("runtimeVariables"); 
    runtime.hashList = "ONE"; 
    runtime.hashList = "ONE"; 
    runtime.hashList = "ONE"; 
    runtime.hashList = "ONE"; 
    runtime.hashList = "ONE"; 
} 
public static void Test_CanExecute(object sender, 
        CanExecuteRoutedEventArgs e) 
{ 
    e.CanExecute = true; 
} 

這是我與結合,我已經綁定的方式,它適用於對其他屬性的文本框,我已經定義了靜態資源的ListView這樣<local:runtimeObject x:Key="runtimeVariables" />App.xaml MainWindow.xaml

<ListView Height="150" Width="400" ItemsSource="{Binding Source={StaticResource runtimeVariables},Path=hashList}"/> 

編輯:

why this?

runtime.hashList = new ObservableCollection<string> { "one" }; shouldn't it be: 

runtime.hashList.Add("one"); ? 

這就是我,雖然,但如果我改變 runtime.hashList = new ObservableCollection<string> { "five" }; 這個 runtime.hashList.Add("one");

那麼如何做我處理,在班級財產?

else 
{ 
    _hashList.Add(value); 
    OnPropertyChanged("hashList"); 
} 

我得到這個錯誤:

Argument 1: cannot convert "System.Collections.ObjectModel.ObservableCollection" to "string"

編輯2: 我希望能夠將一個字符串發送到我的類屬性,這樣我可以簡單的或者是添加新值我的列表或清除它,但它需要在請求時返回列表。

但我不能這樣做我可以嗎?如爲了回報ObservableCollection<string>我需要設置它像這樣:

public ObservableCollection<string> hashList { }

但這並不能讓我只發送字符串數據,因爲它不能字符串轉換爲System.Collections...

如果這是有道理的。

+4

嘗試使用'ObservableCollection'而不是'List' –

+0

或者您可以在每次更改(不是真正正確的方式)後自行創建通知事件:添加項目後的'runtime.OnPropertyChanged(name of(runtimeObject.hashList))''。並且你應該在太遲之前查看[命名指南](https://msdn.microsoft.com/en-us/library/ms229040(v = vs.110).aspx) – Sinatr

+0

'runtime.OnPropertyChanged(「hashList 「)'如果他使用的是舊版本的C# – user2023861

回答

1

在這種情況下,您不需要INotifyPropertyChanged就可以自行更新ListView。該事件在整個對象被替換時使用,即用一個完全不同的集合替換集合。當您將項目添加到集合中或從中刪除項目時,您真正感興趣的是更新ListView。這是與一個完全不同的事件處理:INotifyCollectionChanged。如果您將ItemsSource綁定到實現該事件的集合,則ListView將在收集更改時自動更新。 ObservableCollection實現該事件,以便您要使用的集合。

我會取代你的runtimeObject與此:

public class runtimeObject : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string name) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
     } 

    } 

    private readonly ObservableCollection<string> _hashList = new ObservableCollection<string>(); 
    public ObservableCollection<string> hashList 
    { 
     get { return _hashList; } 
    } 
} 

,然後改變你的Test-Executed方法是:

public static void Test_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
    var runtime = (runtimeObject)Application.Current.TryFindResource("runtimeVariables"); 
    runtime.hashList.Add("one"); 
    runtime.hashList.Add("two"); 
    runtime.hashList.Add("three"); 
    runtime.hashList.Add("four"); 
    runtime.hashList.Add("five"); 
} 
+0

查看更新後的帖子 –

+1

該死的,我一直在想這些都是錯的......謝謝! –

+0

很高興我能幫到你。 –

2

您正在籌集PropertyChangedhashList,但hashList實例並未更改,只有hashList的內容發生了變化。 WPF內置了優化功能,可以防止在對象實際沒有更改時發生任何更新。

如果你想WPF對更改列表的內容作出迴應,你需要火CollectionChanged相反,要做到這一點最簡單的方法就是用ObservableCollection而不是List

+0

剛更新了我的帖子:)得到同樣的問題 –

1

ListView不知道有新值添加到您的列表中,因爲List<T>不實現INotifyPropertyChanged接口。 您有以下選擇:

  • 可以將List<T>類更改爲ObservableCollection<T>類。
  • 可以使該填充列表火OnPropertyChanged你的對象
  • 上,你可以只是填充值列表中選擇您的ListView實例化之前實現後INotifyPropertyChanged
  • 在您自己的列表類。

編輯:

爲什麼呢?

 runtime.hashList = new ObservableCollection<string> { "one" }; 

它不應該是:

 runtime.hashList.Add("one"); 

+0

剛剛更新了我的帖子,得到了同樣的問題 –

+0

更新了我的帖子 –

1

你的問題是在這裏:

var runtime = (runtimeObject)Application.Current.TryFindResource("runtimeVariables"); 
runtime.hashList = new ObservableCollection<string> { "one" }; 
runtime.hashList = new ObservableCollection<string> { "two" }; 
runtime.hashList = new ObservableCollection<string> { "three" }; 
runtime.hashList = new ObservableCollection<string> { "four" }; 
runtime.hashList = new ObservableCollection<string> { "five" }; 
runtime.hashList = new ObservableCollection<string> { "six" }; 

每一次,你正在創建一個新的列表。你想在ObservableCollectionruntime.hashList財產一旦分配,那麼每個字符串添加到集合:

var runtime = (runtimeObject)Application.Current.TryFindResource("runtimeVariables"); 
runtime.hashList.Add("one"); 
runtime.hashList.Add("two"); 
runtime.hashList.Add("three"); 
runtime.hashList.Add("four"); 
runtime.hashList.Add("five"); 
runtime.hashList.Add("six"); 

ObservableCollection類實現另一個名爲INotifyCollectionChanged隨後類似於INotifyPropertyChanged的模式,但它提出了一個OnCollectionChanged事件接口每當集合的內容發生變化時。 WPF監聽對該事件集合的更改並適當地更新顯示。

相關問題