Murtaza Munshi
Murtaza Munshi

Reputation: 1085

How to bind textblock with property

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; }

Upvotes: 2

Views: 489

Answers (5)

terrybozzio
terrybozzio

Reputation: 4532

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;

Upvotes: 0

ansible
ansible

Reputation: 3579

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

}

Upvotes: 2

Farhad Jabiyev
Farhad Jabiyev

Reputation: 26665

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.

Upvotes: 1

dovid
dovid

Reputation: 6481

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}"/>

Upvotes: 0

TMan
TMan

Reputation: 4122

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.

Upvotes: 1

Related Questions