編輯:改變清單的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...
如果這是有道理的。
嘗試使用'ObservableCollection'而不是'List' –
或者您可以在每次更改(不是真正正確的方式)後自行創建通知事件:添加項目後的'runtime.OnPropertyChanged(name of(runtimeObject.hashList))''。並且你應該在太遲之前查看[命名指南](https://msdn.microsoft.com/en-us/library/ms229040(v = vs.110).aspx) – Sinatr
'runtime.OnPropertyChanged(「hashList 「)'如果他使用的是舊版本的C# – user2023861