views:

47

answers:

2

Possible Duplicate:
Why does WPF support binding to properties of an object, but not fields?

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 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?

+3  A: 

It appears it must be a property.

The source of the binding can be any public property, including properties of other controls, common language runtime (CLR) objects, XAML elements, ADO.NET DataSets, XML Fragments, and so forth.

Source

Ragepotato
+3  A: 

Fields are not part of the binding source spec

common language runtime (CLR) objects

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.

dynamic objects

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 objects

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 objects

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.

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 objects

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

Aaron