简体   繁体   English

添加到视图模型的属性不会在组合框中生成项目

[英]Property added to view model doesn't produce items to a combo box

My view model class is very simple. 我的视图模型类非常简单。

class Presenter
{
  public IEnumerable<String> JustFake => new List<String> { "aaa", "bbb" };

  public Presenter() { }
}

IN XAML I've added a combo box bound to the property as follows. 在XAML中,我向属性添加了一个组合框,如下所示。

<Window.DataContext>
  <local:Presenter/>
</Window.DataContext>
...
<ComboBox x:Name="comboBox"
          ItemsSource="{Binding JustFake}"/>

However, although intellisense found JustFake , the values don't appear in the combo box. 但是,尽管intellisense找到了JustFake ,但这些值并未出现在组合框中。 It's empty. 它是空的。 I've tried implementing INotify... etc. but nothing gave me any difference. 我尝试实现INotify ...等,但是没有任何区别。 I can't really tell what I can be missing. 我真的无法说出我可能会错过什么。 Should I inherit something else (I saw a blog where they mentioned BindableBase , whatever that class might be)? 我是否应该继承其他内容(我看到一个博客,他们在其中提到 BindableBase ,无论该类可能是什么)? Should I introduce the Presenter in another way? 我应该以其他方式介绍Presenter吗?

public IEnumerable<String> JustFake = new List<String> { "aaa", "bbb" };

Declares a public field , which cannot be bound against. 声明一个不能绑定的公共字段 You need a property : 您需要一个属性

public IEnumerable<String> JustFake { get; set;}

public Presenter()
{
    JustFake = new List<String> { "aaa", "bbb" };
}

If you want to set the property after the constructor, you would need to utilize INotifyPropertyChanged , and if you want to modify the collection, make sure to use an ObservableCollection instead of List , as it implements INotifyCollectionChanged . 如果要在构造函数之后设置属性,则需要利用INotifyPropertyChanged ,如果要修改集合,请确保使用ObservableCollection而不是List ,因为它实现了INotifyCollectionChanged

Update : You were apparently using a C# 6 expression member, so you may be OK on that front. 更新 :您显然使用的是C#6表达式成员,因此您在那方面可能还可以。

Update 2 : From the exception you reported, it appears that your DataContext is either not set, or being set later to this . 更新2 :从您报告的异常来看,您的DataContext似乎未设置,或者稍后被设置为this Check and make sure nothing is overriding it. 检查并确保没有什么东西能覆盖它。

Binding to ItemSource on an ItemsControl , such as a ListBox , or a DataGrid requires a public property: 绑定到ItemSourceItemsControl ,如ListBoxDataGrid需要的公共属性:

class Presenter
{
  public List<string> JustFake { get; set; }

  public Presenter() 
  {
     JustFake = new List<string> { "aaa", "bbb" };
  }
}

To support notifications back to the UI, the collection requires an INotifyPropertyChanged implementation, it is often easier to use or derive from a collection class that already implements it. 为了支持返回给UI的通知,该集合需要INotifyPropertyChanged实现,通常更容易使用或从已经实现它的集合类派生。 The ObservableCollection<T> : ObservableCollection<T>

class Presenter
{
  public ObservableCollection<string> JustFake { get; set; }

  public Presenter() 
  {
     JustFake = new ObservableCollection<string> { "aaa", "bbb" };
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM