0

how can I do a bind if the property to show is a property from a property, like this case:

Xaml:

<TextBox Text="{Binding log.Message}"/> ???? 

In the class defined as Datacontext, I declare a log variable:

public Log log = new Log(); 

the Log class:

public class Log : INotifyPropertyChanged { public static string Message{ get { return message; } } .... 
2
  • This is pretty much how it should be done. Are you sure you have bound the datacontext correctly in the first place? Commented Oct 2, 2013 at 12:36
  • The static in your property can't work. See my answer to deal with different situations, if the static is intended, you don't need to care about the current DataContext. Commented Oct 2, 2013 at 12:39

3 Answers 3

4

Your question is a bit unclear to me, but i give it a shot:

If the DataContext is an instance of the Log class, and the property is non static. Than the proper binding would be

<TextBox Text="{Binding Message}"/> 

From there you can easily nest your bindings. For example if Log would have an instance of a class

public class Log { public MessageHandler Message {get;set;} } 

which would have a property LocalizedMessage, it would simply be

<TextBox Text="{Binding Message.LocalizedMessage}"/> 

If you want to bind to a static property, which your Message property currently is:

<TextBox Text="{Binding Source={x:Static MyNs:Log.Message}, Path=.}"/> 
Sign up to request clarification or add additional context in comments.

2 Comments

Also worth mentioning, that for first scenario (non-static property) to work, log should be a property, not a field. Which is probably the reason why OP is having problems in the first place.
You a right. I think he confused the name of his variable as a requirement to put into the binding. Which of course is not necessary.
0

You can't bind static properties to XAML. Only .Net 4.5 enables that, and even that with some work. see: WPF 4.5 – Part 9 : binding to static properties. You can find the way there.

If you can't use .Net 4.5, check out this SO thread for another workaround.

Comments

0

The problem with what you wrote is that Message is a static property, so you're not suppose to get it from the log object, but from the Log class:

<Window.Resources> <local:Log x:Key="logClass"/> </Window.Resources>

<TextBox Text="{Binding Source={StaticResource logClass}, Path=Message}"/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.