Home > Enterprise >  Why can't I bind a string in WPF
Why can't I bind a string in WPF

Time:08-31

Trying to wrap my old brain around WPF data binding. Starting with the simplest scenario I could think of: an example online that worked, and I then simplified it. (See code below)

I bound a simple string property to a TextBlock. Didn't work. When I then encapsulated that string inside a class of my own, it worked just fine!

Question: Why doesn't my string property work, but my class does?

<Window x:Class = "DataBindingOneWay.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Height = "350" Width = "600">

    <Grid>
        <StackPanel Orientation = "Vertical" HorizontalAlignment="Center" Margin="0,100,0,0">
            <TextBlock Text="{Binding Name1}" />
        </StackPanel>
    </Grid>

</Window>
using System.Windows;

namespace DataBindingOneWay
{
    public partial class MainWindow : Window
    {
        public class Employee
        {
            public string? Name1 { get; set; } = "Paul";
        }

        public string? Name1 { get; set; } = "Peter";

        public MainWindow()
        {
            InitializeComponent();
            DataContext = new Employee();
//            DataContext = Name1;            
        }
    }
}

CodePudding user response:

Because there is no Name1 inside the object that you are binding to, when you bind to the string. That is, there is no Name1 inside the Name1 (what would be the result of Name1.Name1?). There is, however, a Name1 inside the Employee object (what would be the result of Employee.Name1?)

CodePudding user response:

If you want to access Name1 property of MainWindow you should set DataContext to this (which is MainWindow instance) instead of Name1.

using System.Windows;

namespace DataBindingOneWay
{
    public partial class MainWindow : Window
    {
        public class Employee
        {
            public string? Name1 { get; set; } = "Paul";
        }

        public string? Name1 { get; set; } = "Peter";

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;            
        }
    }
}

or from the other side - when you want the Name1 property to be set as DataContext of MainWindow your binding should look like this:

<Window x:Class = "DataBindingOneWay.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Height = "350" Width = "600">
    <Grid>
        <StackPanel Orientation = "Vertical" HorizontalAlignment="Center" Margin="0,100,0,0">
            <TextBlock Text="{Binding}" />
        </StackPanel>
    </Grid>
</Window>

CodePudding user response:

Thanks to Jonathan's inspiring response, I got to think: WHAT field, or, rather, property in the String Class actually holds the 'value' of the string?

I played with everything, but nothing seemed to hold the actual string! (Weird). That's where I got a crazy idea and tried simply

            <TextBlock Text="{Binding}" />

And

            DataContext = Name1;            

And ... that worked!

Thanks Jonathan. Much appreciated.

(As an aside: I didn't know you can actually vote down QUESTIONS, as just happened with my question. To the person who did that, I humbly offer my apologies for having insulted your intelligence by trying to learn something (which, btw, I thought, was the whole point of this site!)).

  • Related