Is it possible to bind to a field?
Colleagues,
I have a text box which binds to data:
Code:
<TextBox x:Name="textBox1" Text="{Binding Path=Comments}" Width="200" Margin="0,4" />
If Comments is declared as a property, the binding works:
Code:
public int Comments { get; set; }
However, if Comments is declared as a field, the binding doesn’t work:
Code:
public int Comments;
I haven’t seen an example where a control was bound to a field, but I haven’t seen any indication that it’s a bad practice or not possible.
- Nick
Re: Is it possible to bind to a field?
As far as I know, it is not possible to bind to field. The purpose is simple: fields are implementation details, not parts of object's contract. Making field accessible from outside the class breaks encapsulation, so is bad practice. Despite the fact that there is not way how to let clients of the field know e.g. that the value changed.
Re: Is it possible to bind to a field?