0

I am attempting to bind a TextBlock's text to a string variable located in a instanced class that comes from a separate library (DLL), but I am new to WPF and it's binding model so I'm not quite sure how this is done or if it is possible. I'd like to stay away from setting the text in codebehind as I like the idea of the binding model better.

(Example of the class) From a separate library:

public class LoggedInUser
{
     public string Username { get; }
}

And then elsewhere in the program:

public static LoggedInUser MyUser;

So basically,

 <TextBlock Text="{Binding MyUser.Username}" />

Where MyUser is an instanced class stored elsewhere in the program.

This is what I am essentially trying to do: Simply bind TextBlock's text to a string that is in an instance of a class that comes from a separate library and the instance of the class is stored in my program elsewhere.

Anyone have any ideas? Would the text change when the value changes?

Thank you.

  • possible duplicate: https://stackoverflow.com/questions/14624373/wpf-textblock-text-binding – jazb Dec 29 '18 at 05:04
  • You are telling that you `like the idea of the binding model` but you binding to a static member of some class instead of binding to a property of your model? – vasily.sib Dec 29 '18 at 05:18
  • @vasily.sib That's the attitude! – XBLToothPik Dec 29 '18 at 05:43
  • 1
    You will find tons of tutorials on the web. Instead of asking someone to write one for you take a look at some existing and then come back with more specialized questions. This way, I promise you, you will learn much faster. https://www.codeproject.com/Articles/26210/Moving-Toward-WPF-Data-Binding-One-Step-at-a-Time . MSDN is a very good source too: https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-overview#creating-a-binding – BionicCode Dec 29 '18 at 05:46
  • @BionicCode I never asked for anyone to write me any code, I appreciate feedback however and will look more into it, thanks. – XBLToothPik Dec 29 '18 at 05:52
  • _"Would the text change when the value changes?"_ - no, unless your `MyUser` is implement [INotifyPropertyChanged](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netframework-4.7.2) and it raises the `PropertyChanged` event on `Username` change. – vasily.sib Dec 29 '18 at 05:57
  • That's what I figured but I just had to ask it for clarification, thank you :) – XBLToothPik Dec 29 '18 at 06:05

0 Answers0