开发者

Why to use Path in silverlight databinding?

I am new to the silverlight. I found some articles on the internet for databinding. I can see that binding is performed in different ways as follows

<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Path=Name}"/>
<TextBlock Text="{Binding Path=Account}"/>
<TextBlock Text="{Binding Path=Property1.Property2.Property3}"/>

In the first textblock binding is performed with property name. In second example also binding is performed with property name. Then what is different with Path in second textbl开发者_StackOverflowock ? I know how to do binding for the first three textblock but I am not aware how to do binding with the fourth textblock ? and when we use the binding of the following type

<TextBlock Text="{Binding Path=Property1.Property2.Property3}"/>

Can you please give me coding example along with explanation for all above thing ? If I misunderstood something then please guide me.


By using the following binding:

<TextBlock Text="{Binding Path=Property1.Property2.Property3}"/>

You're saying that the data context of the TextBlock has a property called Property1, which returns an object that has a property called Property2, which in turn has a property called Property3. The value returned by Property3 is what will be shown in the TextBox.

For example, if your classes looked like this and the data context for the TextBox was an instance of Foo, you'd see "Hello World" displayed:

public class Foo
{
   public Foo1 Property1 { get; set; }
}

public class Foo1
{
   public Foo2 Property2 { get; set; }
}

public class Foo2
{
   public string Property3 { get { return "Hello World"; } }
}

The "." syntax just lets you refer to "subproperties" of an object. For more information, take a look at this MSDN article.


There is no difference between the first and the second TextBlock in your question.

Path is the 'default' property of the Binding object that gets set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜