添加到一个的ObservableCollection

添加到一个的ObservableCollection <string>

问题描述:

编辑:改变清单的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

在这种情况下,您不需要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

很高兴我能帮到你。 –

您正在筹集PropertyChangedhashList,但hashList实例并未更改,只有hashList的内容发生了变化。 WPF内置了优化功能,可以防止在对象实际没有更改时发生任何更新。

如果你想WPF对更改列表的内容作出回应,你需要火CollectionChanged相反,要做到这一点最简单的方法就是用ObservableCollection而不是List

+0

刚更新了我的帖子:)得到同样的问题 –

ListView不知道有新值添加到您的列表中,因为List<T>不实现INotifyPropertyChanged接口。 您有以下选择:

  • 可以将List<T>类更改为ObservableCollection<T>类。
  • 可以使该填充列表火OnPropertyChanged你的对象
  • 上,你可以只是填充值列表中选择您的ListView实例化之前实现后INotifyPropertyChanged
  • 在您自己的列表类。

编辑:

为什么呢?

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

它不应该是:

 runtime.hashList.Add("one"); 

+0

刚刚更新了我的帖子,得到了同样的问题 –

+0

更新了我的帖子 –

你的问题是在这里:

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监听对该事件集合的更改并适当地更新显示。