0

I am just new to WPF.

I have a wpf app and there i simply have a dock panel and inside dock panel i have a textblock.

I want to bind the text property of textblock to my custom object's property but that' not working.

I think i am missing something here but don't know what.

Here is the code snippet.

      <TextBlock Text="{Binding Source=myDataSource, Path=ColorName}"/>
</DockPanel>

My custom class.

class MyData
    {
        public string ColorName { get; set; }
}

and main window constructor..

public partial class MainWindow : Window
    {
        MyData myDataSource;
        public MainWindow()
        {
            InitializeComponent();
            myDataSource = new MyData { ColorName = "Red" };
    }
}

3 Answers 3

1

myDataSource needs a get and set. You also need to set the dataContext for the window, so it should be-

public partial class MainWindow : Window
{
    public MyData MyDataSource { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        MyDataSource = new MyData { ColorName = "Red" };
    }
}

public class MyData
{
    public string ColorName { get; set; }
}

and xaml code should be -

<TextBlock Text="{Binding MyDataSource.ColorName}"/>

edit Sorry got this wrong I've changed to the correct code

Sign up to request clarification or add additional context in comments.

4 Comments

I did that but even I don't see value "Red" in my TextBlock's Text property
@Praveen sorry about that, fixed it now
Thanx Quinn351, can u please tell me what is DataContext and y it was necessary to add ?
DataContext sets the base object that is used as reference for bindings within the component's children if there is no other source specified, if the DataContext had not been set the binding would need to somehow reference the window e.g. via ElementName (see my answer). "MyDataSource.ColorName" without any qualifying property is equivalent to "Path=MyDataSource.ColorName" by the way.
0
  1. What you bind to needs to be a public property. (Your data-object needs to satisfy that condition as well)
  2. Unless you set the property before InitializeComponent() the binding will might not update depending on your binding.
  3. If you set it again at any point in time after the initilization and want the binding to be updated you should implement INotifyPropertyChanged or work with dependency properties.
  4. Since the data is in your window you might need to access it over that: {Binding ElementName=window, Path=myDataSource.ColorName}

Comments

0

If you only want to bind to MyData, don't set window as its own DataContext. Istead, set the data you're binding to. This way it's more clear what is data, and what is view.

Also, lose the Source on Binding, you won't generally need it.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MyData { ColorName = "Red" };
    }
}

public class MyData
{
    public string ColorName { get; set; }
}

XAML:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.