简体   繁体   English

似乎无法在我脑海中获得WPF DataBinding

[英]Can't seem to get WPF DataBinding in my head

Ok, I'm no newbie at programming or C# as such, I just can't seem to get WPF's databinding straight in my head. 好吧,我不是编程或C#的新手,我似乎无法将WPF的数据绑定直接放在脑海中。 My colleagues are raving about it (and yes, I will ask them as well) but right now I'm stumped. 我的同事们对此赞不绝口(是的,我也会问他们)但是现在我很难过。

Here's what I'd like to do for starters: 这是我想为初学者做的事情:

As an example I've got a list of things like this: 作为一个例子,我有一个这样的事情列表:

List<Thing> thingList = Source.getList();

Now normally I'd go 现在通常我会去

foreach(Thing t in thingList)
{
    //add thing to combobox
}

But from what I can gather is that I can somehow not do this but use a databinding to populate the combobox for me. 但是从我可以收集到的是,我可以以某种方式不这样做,但使用数据绑定为我填充组合框。

What I can't seem to get is where do I put the 'thingList'? 我似乎无法得到的是我在哪里放'thingList'? Do I make it a separate property somewhere? 我是否将它作为一个单独的财产? Where do I put that property? 我在哪里放这个房产?

I feel very stupid at the moment, as I've been struggling with this for a while now and I can't find any examples out there that make me understand this - probably very simple - concept. 我觉得此刻非常愚蠢,因为我现在已经挣扎了一段时间,而且我找不到任何让我理解这个 - 可能非常简单 - 概念的例子。

Anyone out there willing to help me or point me at some step-by-step guide I might have missed? 那里的任何人都愿意帮我或指点我可能错过的一步一步指导?

Use ObservableCollection<T> for databinding in WPF. 使用ObservableCollection<T>在WPF中进行数据绑定。 Where T is your class. 哪个T是你的班级。 See example below 见下面的例子

public class NameList : ObservableCollection<PersonName>
{
    public NameList() : base()
    {
        Add(new PersonName("A", "E"));
        Add(new PersonName("B", "F"));
        Add(new PersonName("C", "G"));
        Add(new PersonName("D", "H"));
    }
  }

  public class PersonName
  {
      private string firstName;
      private string lastName;

      public PersonName(string first, string last)
      {
          this.firstName = first;
          this.lastName = last;
      }

      public string FirstName
      {
          get { return firstName; }
          set { firstName = value; }
      }

      public string LastName
      {
          get { return lastName; }
          set { lastName = value; }
      }
  }

Now in XAML. 现在在XAML。 Go to resource tag 转到资源标记

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  xmlns:c="clr-namespace:SDKSample"

  x:Class="Sample.Window1"
  Width="400"
  Height="280"
  Title="MultiBinding Sample">

  <Window.Resources>
    <c:NameList x:Key="NameListData"/>
  </Window.Resources>


<ListBox Width="200"
         ItemsSource="{Binding Source={StaticResource NameListData}}"  // Name list data is declared in resource
         ItemTemplate="{StaticResource NameItemTemplate}"
         IsSynchronizedWithCurrentItem="True"/>

xmnls:c will give you option to choose the class. xmnls:c将为您提供选择课程的选项。 Here you can choose c,d ,ex whatever but be sure it should be used earlier 在这里你可以选择c,d,ex,但要确保它应该在之前使用

When it comes to data-binding i've found that this page if read thoroughly answers most of the questions beginners have about it. 说到数据绑定,我发现这个页面如果仔细阅读,可以回答初学者对它的大部分问题。

To answer the question: The alernative to adding all the items is to tell the ComboBox where to get its items from, this is done with the ItemsSource property. 要回答这个问题:添加所有项目的alernative是告诉ComboBox从哪里获取其项目,这是通过ItemsSource属性完成的。

You can either set this in XAML or in code, while you would need a binding expression in XAML a normal assignment should do in code: 您可以在XAML或代码中设置它,而在XAML中需要一个绑定表达式,正常的赋值应该在代码中执行:

comboBox.ItemsSource = thingList;

If you do not specify further how those objects in the list are to be displayed the ToString method will be called, unless overridden you will usually end up with the class-path of your object. 如果不进一步指定列表中的这些对象将如何显示,则将调用ToString方法,除非被覆盖,否则通常会以对象的类路径结束。 There are two main ways of telling the application how to display the object: 告诉应用程序如何显示对象有两种主要方式:

The fist and more heavy option is Data Templates which is used for displaying complex data using controls (which in turn can have items and templates etc), the second way is using lightweight properties like DisplayMemberPath , which should be set to the name of the property which should be displayed (usually just a string). 第一种也是更重的选项是数据模板 ,用于使用控件显示复杂数据(反过来可以包含项目和模板等),第二种方式是使用DisplayMemberPath等轻量级属性,应该将其设置为属性的名称应该显示(通常只是一个字符串)。

If your list changes and the combo box should be updated on its own the source should implement INotifyCollectionChanged , in most cases you will use the standard implementation ObersableCollection<T> . 如果您的列表发生更改并且组合框应自行更新,则源应实现INotifyCollectionChanged ,在大多数情况下,您将使用标准实现ObersableCollection<T>

Most people would use WPF Databinding to populate the combobox but you don't have to. 大多数人会使用WPF数据绑定来填充组合框,但您不必这样做。

You can add the items via code if you want to. 如果您愿意,可以通过代码添加项目。

It's easy to get trapped into doing everything as it "should" be done without have a good reason for doing it that way (BTW, I'm not recommending you manually add the items, I'm just saying you can). 很容易被困在做所有事情,因为它“应该”完成没有充分的理由这样做(顺便说一下,我不建议你手动添加项目,我只是说你可以)。

List<Thing> thingList = Source.getList();

foreach(Thing t in thingList)
{
   combobox.Items.Add( t );
}

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

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