简体   繁体   中英

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

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

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.

您编写的内容存在问题,即Message是一个静态属性,因此您不是想从log对象中获取它,而是从Log类中获取它:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM