为什么我不能绑定到 WPF 中类上的字段而不是绑定到属性 [英] Why can't I bind to a field on a class in WPF instead of binding to a property

查看:17
本文介绍了为什么我不能绑定到 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?

这是一个示例片段:

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

WPF 代码段:

<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 接口.如果您可以在代码中访问该成员,你可以绑定它.例如,如果一个动态对象使您能够访问代码成员通过someObjet.AProperty,你可以绑定到它通过将绑定路径设置为A属性.

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 或元素.便捷的访问方式作为绑定源的 XML 数据标记是使用 XmlDataProvider目的.有关更多信息,请参阅如何to:绑定到 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目的.有关更多信息,请参阅如何to:绑定到 XDocument、XElement 或LINQ for 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.

这篇关于为什么我不能绑定到 WPF 中类上的字段而不是绑定到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆