CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2001
    Silicon Valley

    Is it possible to bind to a field?


    I have a text box which binds to data:
    <TextBox x:Name="textBox1" Text="{Binding Path=Comments}" Width="200" Margin="0,4" />
    If Comments is declared as a property, the binding works:
    public int Comments { get; set; }
    However, if Comments is declared as a field, the binding doesnít work:
    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

  2. #2
    Join Date
    Mar 2004
    Prague, Czech Republic, EU

    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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: Is it possible to bind to a field?

    It isn't possible.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.