开发者

Setting width of a TextBlock vs a Grid

If there's a TextBlock inside a Grid, what's the best way (performance wise) to set its width and height?

Is setting them in the TextBlock's properties will be better than setting it as Grid's properties?

basically 开发者_如何学Pythonwhat I'm asking is which one of the following is better :

<Grid Width="200" Height="200">
    <TextBlock />
</Grid>

vs

<Grid>
    <TextBlock Width="200" Height="200"/>
</Grid>


<Grid Width="200" Height="200">
    <TextBlock />
</Grid>

Using a Grid purely to constrain a TextBox is not really a good way to set the width of the TextBox. A Grid is more for laying out multiple controls. However this is perfectly acceptable:

<Grid Width="200" Height="200">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <TextBlock Grid.Column="1" HorizontalAlignment="Stretch" Margin="5,0" />
</Grid>

This is along the same lines as what @Ahmed suggested.

Generally with a XAML layout the recommendation is to use a proportional layout rather than fixed sizes, so that your UI can be resized correctly with minimal oversight from any of your code. This means it is good to let the parent element dictate the size as much as possible, only used fixed sizes where you must. (Setting MinWidth/MaxWidth/etc is fine, just try to avoid explicitly setting Width where possible, the same goes for the Height properties).


I Really don't understand specifically what you are asking about !


But I really like to set my Grid to be stretched as Window and set my TextBox Width & Height to Auto Or NAN ,, then define special margins & alignments to my Textbox


In the Layout System article on MSDN there is a small section on performance but there is also a dedicated article on optimizing performance of the layout and design. Maybe there is something helpful in there.

It seems to me that this documentation does not make any claims about this issue though, it probably does not matter or the difference is negligibly small.


I don't have any stats to back this up but since WPF's rendering/layout engine works by first measuring the child elements of everything, one would assume if you manually define the size of a child element it would have a marginal effect.

In this scenario, if all you're doing is wrapping a single TextBlock element, you might be better off using something like a <Border>. If there's multiple elements, see if you can use a <StackPanel>.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜