Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to bind a variable of type string to a labals Content control. Any help regarding binding using converters and different kinds of binding would be appreciated.

What I have tried:

I've tried is binding the Window property to itself and trying to access code behind variables by simple binding to them as such:
XAML code
XML
<Window DataContext="{RelativeSource Self}" >
    <Label Content="{Binding Path=Hello}"/>
</Window>

C# code
C#
public partial class MainWindow : Window
{
    string Hello = "Hello World";
    public MainWindow()
    {
    }
}

This does compile but wont display anything. Any feedback would be appriciated.
Posted
Updated 22-Apr-20 21:08pm
v4

1 solution

Hello is both private and a field.
Either of those will break a binding.
It needs to be a public (or internal) property.
Try:
C#
public partial class MainWindow : Window
{
    private const string HelloMessage = "Hello World";
    public string Hello { get { return HelloMessage; } }

    public MainWindow()
    {
    }
}


=============
Edit: MTH - April 29, 2016

Showing binding to a property in such a way as to allow for the Label to update on changes:
XAML:
XML
<Window>
    <Label Content="{Binding Path=Hello}"/>
</Window>
C#:
C#
using System.ComponentModel;
using System.Runtime.CompilerServices;
public partial class MainWindow : Window, INotifyPropertyChanged
{
  public MainWindow()
  {
    InitializeComponent();
    DataContext = this;
  }
  private string _Hello = "Select Workflow Variant:";
  public string Hello
  { 
    get { return _Hello; }
    set
    { 
      _Hello = value;
      OnPropertyChanged();
    }
  }
  #region INotifyPropertyChanged Members

  public event PropertyChangedEventHandler PropertyChanged;

  /// <summary>
  /// Raises this object's PropertyChanged event.
  /// </summary>
  /// <param name="propertyName">The property that has a new value.</param>
  protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
  {
    PropertyChangedEventHandler handler = this.PropertyChanged;
    if (handler != null)
    {
      var e = new PropertyChangedEventArgs(propertyName);
      handler(this, e);
    }
  }
  #endregion
}

Now, if the Hello property is changed, the Label will be updated. Define other settable properties similarly.
 
Share this answer
 
v2
Comments
JONLIN 29-Apr-16 18:19pm    
Thank you very much :) this is the first progress I've had for quite some time. Anyhow is there any reason behind that I shouldn't be able to acces HelloMessage directly but rather that I have to access it through a field?
Also how do I get the Label to auto-update if the source change? Becouse I found that the Const keyword isnt required but that it doesnt update the Contet property if the source changes.
Still thank you for taking youre time :)
Matt T Heffron 29-Apr-16 20:07pm    
It is just one of the constraints of WPF that bindings must be to a property, not a field (or const).
For WPF controls to notice when the property to which they are bound has changed value, the DataContext (the Window itself in this case) must implement the INotifyPropertyChanged interface, and then raise the PropertyChanged event whenever a property value changes.
See my updated Solution, above.
Member 14810750 23-Apr-20 3:09am    
Hello.
I know a long time has passed since you wrote this, and maybe things went forward, but I'm wondering if you can help me.
I'm having some problems with the OnPropertyChanged method, as the handler always returns null.

Any idea how to fix this?
#realJSOP 23-Apr-20 7:04am    
Post this as a question.
Member 14810750 23-Apr-20 11:04am    
Already fixed :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900