If I don’t add explicit accessors to a String property, then data binding doesn’t work. Why is that?
Here is a simple example where a text box is hooked up to a String property.
MainPage.xaml:
<Grid Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="{Binding Message} />
</Grid>
And the code behind:
public String Message;
public MainPage()
{
InitializeComponent();
Message = "Hello World";
DataContext = this;
}
This does not work, the text box is empty.
However, add property accessors;
public String Message { get; set; };
And now it works.
I can’t see this explained in MSDN Data Binding. Can someone explain it? Don’t properties have an implict set/get accessors? Even so, why can’t data binding just access the property?
Thanks,
This is a field, not a property. Adding getters and setters creates an auto property.
Only properties can be bound to.