2

I created a textblock in .xaml and declared a property in the .cs file named WrodName. How do I bind that property with textblock. I need the code which we write in the xaml code of tag i.e. DataContext code. Till now I came up with this

<TextBlock Text="{Binding WordName}"/>

And in .cs file:

public String WordName { get; set; }
Ringo
  • 3,795
  • 3
  • 22
  • 37
Murtaza Munshi
  • 1,065
  • 4
  • 13
  • 40

5 Answers5

2

You will need to set the DataContext. A quick way of doing this is to set it in the constructor DataContext = this;

Also you will need to either set the property before you call InitalizeComponent() or preferably implment INotifyPropertyChanged. Otherwise your UI will not update when the property is set.

See - XAML: Binding a property in a DataTemplate

A quick example

class YourClass : INotifyPropertyChanged
{
        private String _wordName;
        public String WordName 
        {
            get { return _wordName; }
            set
            {
                if (_wordName != value)
                {
                     _wordName= value;
                     OnPropertyChanged("WordName");
                }

            }
        }

        /// <summary>
        /// Raises the PropertyChanged notification in a thread safe manner
        /// </summary>
        /// <param name="propertyName"></param>
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

}
Community
  • 1
  • 1
ansible
  • 3,569
  • 2
  • 18
  • 29
  • Thanx a lot for this code. It is helping me understand how to implement INotifyPropertyChanged – Murtaza Munshi Dec 18 '13 at 17:54
  • Yeah, without it even if you binding is correct, your UI will not update without it. If you set your property before `InitializeComponent()` but it doesn't update after that it's probably something wrong with INotifyPropertyChange. This is the pattern we use in our production code. I think it works pretty well. – ansible Dec 18 '13 at 21:04
1

Set DataContext from xaml:

<Window x:Class="ApplicationName"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

But in that case you m=must assign the value before Initialize():

   WordName = "word";
   InitializeComponent();

Or from code-behind like that:

this.DataContext = this;

But anyway I am recommending you to use MVVM architecture with INOtifyPropertyChanged event. In that case UI will update whenever property is set to a new value.

Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
1

Create a ViewModel class

public class MyViewModel
{
  public String WordName { get; set; }
}

Then in your code behind of your view set the itemssource

public class MyView
{
   this.DataContext = new MyViewModel();

}

There are diff ways to set the datacontext either in code or in xaml. Really an preference thing.

TMan
  • 4,044
  • 18
  • 63
  • 117
0

This will bind to your property:

<TextBlock Text="{Binding WordName,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"/>

Also set DataContext in your ctor:

this.DataContext = this;
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
0

Also, by ElementName. first naming the window:

<Window x:Class="ApplicationName" x:Name=AnyName ...

then bind to window element:

<TextBlock Text="{Binding WordName, ElementName=AnyName}"/>
dovid
  • 6,354
  • 3
  • 33
  • 73