Beetlejuice
Beetlejuice

Reputation: 4425

Binding property from a property

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; } }
  ....

Upvotes: 0

Views: 102

Answers (3)

Programmer
Programmer

Reputation: 2123

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}"/

Upvotes: 0

CKII
CKII

Reputation: 1486

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.

Upvotes: 0

dowhilefor
dowhilefor

Reputation: 11051

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=.}"/> 

Upvotes: 4

Related Questions