开发者

Specifying DataContext in XAML and using it for binding in the next attribute

开发者 https://www.devze.com 2023-03-06 05:17 出处:网络
Consider the following code: <MyControl DataContext=\"{Binding DataObject}\" Text=\"{Binding DisplayText}\"/开发者_如何学Go>

Consider the following code:

<MyControl DataContext="{Binding DataObject}" Text="{Binding DisplayText}"/开发者_如何学Go>

As far as I know, XAML attributes are assigned by the order they are declared. However, XML-wise, depending on it is a bad design. I guess I could rewrite it to look like so:

<Grid DataContext="{Binding DataObject}">
    <MyControl Text="{Binding DisplayText}"/>
</Grid>

However, putting a Grid just for semantics seems wrong to me.

What do you think?

Thanks!

Vitaly


I'm a little confused by your question. The order in which those attributes are set doesn't really make a difference. When the text attribute is set to the binding as you've done it, all it does is create a Binding that is tied to the Text property and has a Path of "DisplayText". As long as the DataContext is set at the time that the binding is evaluated (which occurs sometime later) then you're fine.


Grid is not recommended anyway cause it's effect performance, if you have no chiose(no parent to put context in) use a border

0

精彩评论

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