I’m trying to bind a string to my textbox control, but the string is buried away.
The code I want to use is the following contrived example
namespace BackUps.Logging.ViewModel
{
class Obj1
{
public Obj2 obj2 { get; set; }
}
class Obj2
{
public Obj3 obj3 { get; set; }
}
class Obj3
{
public string Message
{
get { return "Hello World"; }
}
}
My VM looks like
namespace BackUps.Logging.ViewModel
{
internal class LogsVM
{
public Obj1 Obj1 { get; private set; }
public LogsVM()
{
Obj1 = new Obj1();
}
}
My question is, how can I bind the Message to the TextBlock with Xaml? This is what I have:
<Window x:Class="BackUps.Logging.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myData ="clr-namespace:BackUps.Logging.ViewModel"
Title="Logging Results" Height="350" Width="525">
<Grid DataContext="{x:Type myData:LogsVM}">
<TextBlock Text="{Binding Message}" />
</Grid>
</Window>
The above doesn’t work. Nor does
<TextBlock Text="{Binding Obj1.Message}" />
or
<TextBlock Text="{Binding Obj1.Obj2.Obj3.Message}" />
I know this example is daft, but there are many times when a class’s properties is a List type, and within that List is another etc, and knowing how to drill down to a specific property, no matter how many layers deep is important but I’ve gotten no where finding out how.
This works assuming the datacontext is your viewmodel:
You can set up designtime data using this syntax:
d:DataContext="{d:DesignInstance Type=wpfGridColumns:LogsVM, IsDesignTimeCreatable=True}"It gives Intellisense in the VS designer and is helpful when placing bindings.
I used this code behind: