为什么我不能绑定到字段上一类在WPF,而不是绑定到一个属性绑定、字段、而不是、属性

2023-09-02 21:41:13 作者:枉然.

可能重复:   Why并支持WPF绑定到一个对象的属性,而不是字段?

目前看来,在WPF我不能绑定到一个公共领域的对象,但仅限于公共属性。这是WPF的一部分,有意的设计决策,还是我刚刚起步的语法错了吗?

It appears that in WPF I cannot bind to a public field on an object, but only to public properties. Is this an intentional design decision on the part of WPF, or am I just getting the syntax wrong?

下面是一个示例代码段:

Here's a sample snippet:

public class User
{
  public string Username;
  public string FullName;
  public string DisplayName
  {
    get { return FullName; }
  }
}

WPF片段:

WPF Snippet:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="User Tool" >
  <Window.Resources>
    <DataTemplate x:Key="UserTemplate">
      <TextBlock Text="{Binding Path=DisplayName}" />
    </DataTemplate>
  </Window.Resources>
  <ListBox Name="listBoxUsers" ItemTemplate="{StaticResource UserTemplate}" ItemsSource="..." />
</Window>

如果我更改绑定路径用户名或真实姓名,没有显示在屏幕上。是否有替代语法绑定到字段,或者是约束只允许在属性?

If I change the Binding Path to Username or FullName, nothing shows up on screen. Is there an alternate syntax to bind to fields, or is binding only allowed on properties?

推荐答案

字段是不是的绑定源规范

公共语言运行库(CLR)对象

您可以绑定到公共属性,   子的属性,以及索引器,   任何公共语言运行时(CLR)   目的。绑定引擎使用CLR   反思得到的值   属性。另外,对象   实现ICustomTypeDescriptor   或已注册   TypeDescriptionProvider还与   绑定引擎

You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object. The binding engine uses CLR reflection to get the values of the properties. Alternatively, objects that implement ICustomTypeDescriptor or have a registered TypeDescriptionProvider also work with the binding engine.

有关如何更多信息   实施,可以作为一类   绑定源,请参见实现   为绑定源后来在班   这个话题。

For more information about how to implement a class that can serve as a binding source, see Implementing a Class for the Binding Source later in this topic.

动态对象

您可以绑定到可用的属性   一个对象和索引器   实现   IDynamicMetaObjectProvider接口。   如果可以访问该成员在code,   可以绑定到它。例如,如果一个   动态对象,您可以访问   通过成员在code   someObjet.AProperty,可以绑定到   它通过结合路径设定到   AProperty。

You can bind to available properties and indexers of an object that implements the IDynamicMetaObjectProvider interface. If you can access the member in code, you can bind to it. For example, if a dynamic object enables you to access a member in code via someObjet.AProperty, you can bind to it by setting the binding path to AProperty.

ADO.NET对象

您可以绑定到ADO.NET对象,例如   作为数据表。 ADO.NET的数据视图   实现了IBindingList的接口,   它提供了更改通知   该绑定引擎侦听

You can bind to ADO.NET objects, such as DataTable. The ADO.NET DataView implements the IBindingList interface, which provides change notifications that the binding engine listens for.

XML对象

您可以结合并运行XPath查询   在一个XmlNode,XmlDocument的,或   的XmlElement。一个方便的方式来访问   XML数据是在绑定源   标记是使用XmlDataProvider   目的。有关详细信息,请参阅如何   于:绑定到XML数据使用   XMLDataProvider和XPath查询。

You can bind to and run XPath queries on an XmlNode, XmlDocument, or XmlElement. A convenient way to access XML data that is the binding source in markup is to use an XmlDataProvider object. For more information, see How to: Bind to XML Data Using an XMLDataProvider and XPath Queries.

您还可以绑定到的XElement或   的XDocument,或绑定到结果   查询这些类型的对象上运行   使用LINQ to XML。一个方便的方式   使用的LINQ to XML来访问XML数据   这是标记绑定源   是使用的ObjectDataProvider   目的。有关详细信息,请参阅如何   于:绑定到的XDocument,的XElement,或   LINQ对XML的查询结果。

You can also bind to an XElement or XDocument, or bind to the results of queries run on objects of these types by using LINQ to XML. A convenient way to use LINQ to XML to access XML data that is the binding source in markup is to use an ObjectDataProvider object. For more information, see How to: Bind to XDocument, XElement, or LINQ for XML Query Results.

DependencyObject的对象

您可以绑定到依赖属性   任何DependencyObject的。对于   例如,请参阅如何绑定   两个控件的属性。

You can bind to dependency properties of any DependencyObject. For an example, see How to: Bind the Properties of Two Controls.