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?