1

I am using wpf. I want to bind a textbox with a simple string type value initialized in xaml.cs class. The TextBox isn't showing anything. Here is my XAML code:

<TextBox Grid.Column="1" Width="387" HorizontalAlignment="Left" Grid.ColumnSpan="2" Text="{Binding Path=Name2}"/>

And the C# code is this:

public partial class EntitiesView : UserControl
{
    private string _name2;
    public string Name2
    {
        get { return _name2; }
        set { _name2 = "abcdef"; }
    }
    public EntitiesView()
    {
        InitializeComponent();
    }
}
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Dua Ali
  • 3,163
  • 3
  • 19
  • 13

3 Answers3

8

You never set the value of your property. Simply defining set { _name2 = "abcdef"; } does not actually set the value of your property until you actually perform the set operation.

You can change your code to look like this for it to work:

public partial class EntitiesView : UserControl
{
    private string _name2;
    public string Name2
    {
        get { return _name2; }
        set { _name2 = value; }
    }

    public EntitiesView()
    {
        Name2 = "abcdef";
        DataContext = this;
        InitializeComponent();
    }
}

Also, as people have mentioned, if you intend to modify your property's value later on and want the UI to reflect it, you'll need to implement the INotifyPropertyChanged interface:

public partial class EntitiesView : UserControl, INotifyPropertyChanged
{
    private string _name2;
    public string Name2
    {
        get { return _name2; }
        set
        {
            _name2 = value;
            RaisePropertyChanged("Name2");
        }
    }

    public EntitiesView()
    {
        Name2 = "abcdef";
        DataContext = this;
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • Sorry about replying to this so late, but how does a form control subscribe to the PropertyChangedEventHandler? – William May 27 '14 at 22:14
2

Just add this line in your EntitiesView constructor

DataContext = this;
UsmanAzam
  • 539
  • 4
  • 15
0

Why dont you add a view model and keep your property there ?

View Model class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfApplication1
{
    public class TestViewModel : INotifyPropertyChanged
    {
        public string _name2;
        public string Name2
        {
            get { return "_name2"; }
            set
            { 
                _name2 = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Name2"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
    }
}

EntitiesView User Control

public partial class EntitiesView : UserControl
{

    public EntitiesView()
    {
        InitializeComponent();
        this.DataContext = new TestViewModel();
    }
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65