I am trying to Bind a public member variable of an object to a TextBox in the UI. But when I execute the application the TextBox does not display anything.
But when I make the member variable as private and provide get set accessors to access the member variable the application works fine.
So my question is whether we can bind a public member variable of an object to a property of some other object?

This does not work:

public class Book
   {
      public string title;
   }

This works:

public class Book
   {
      private string title;
      public string Title
      {
         get { return title; }
         set { title = value; }
      }
   }

Please clear my doubt...
Hope the question is clear....

Recommended Answers

All 4 Replies

Book B = new Book();
            B.title = "The book";
            MyTextBox.Text = B.title;

works fine with me.
But it is better practice to keep fields of a class private and to use property syntax.
You could in this case even use automatic syntax(the compiler takes care of the details)
So your second code sample would become:

class Book
{
    public string title { get; set; }
}

I am pleased to receive your reply so quickly...

With reference to your code:

Book B = new Book();
            B.title = "The book";
            MyTextBox.Text = B.title;

works even for me when I directly write to the TextBox (i.e. MyTextBox.Text = B.title).

But I am trying to bind the "Text Property" of the TextBox in XAML as follows:

<TextBox HorizontalAlignment="Right" Margin="0,164,155,0" VerticalAlignment="Top" Width="110" Height="46" [B]Text="{Binding Title}"[/B] TextWrapping="Wrap"/>

The above code works if Title is being accessed through get/set but is not working if I make Title a public member and do not use property syntax to access that.

The following code will show what I am trying to say...

Works Fine:

namespace ControlBindingExp_1
{
	public partial class MainWindow : Window
	{
		private Book book1 = new Book();
		public MainWindow()
		{
			this.InitializeComponent();
			book1.Title = "Dilake";
			LayoutRoot.DataContext = book1;
		}
	}
	public class Book
	{
		private string title;
		public string Title
		{
			get { return title; }
			set { title = value; }
		}
	}
}

Does not work:

namespace ControlBindingExp_1
{
	public partial class MainWindow : Window
	{
		private Book book1 = new Book();
		public MainWindow()
		{
			this.InitializeComponent();
			book1.Title = "Dilake";
			LayoutRoot.DataContext = book1;
		}
	}
	public class Book
	{
		public string Title;
	}
}

XAML part is same for both cases:

<TextBox HorizontalAlignment="Right" Margin="0,164,155,0" VerticalAlignment="Top" Width="110" Height="46" Text="{Binding Title}" TextWrapping="Wrap"/>

I agree with your advice that:
"It is better practice to keep fields of a class private and to use property syntax.
You could in this case even use automatic syntax(the compiler takes care of the details)"

But I just want to know why the "not so correct" approach is not working....

Thanks in advance....

>>But I just want to know why the "not so correct" approach is not working....

Danny already gave you the answer. The best practices for .NET development is to not expose fields publicly, only properties. Allowing you to bind to public properties (with get/set) and data sources helps enforce those best practices. With object oriented programming your business entities should always encapsulate members to ensure you can fire events and perform validation when an objects' state changes.

Your statement:
"to ... fire events and perform validation when an objects' state changes."
cleared my Doubt because yes in DataBinding properties of two elements are bound together by firing events to perform validation when one of the objects' state changes.... and here I assume that an object's state change fires an event only when we provide get/set property syntax to access the variable of that object....

Anyhow... Thanks a lot to both of You...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.