2015-04-21 32 views
0

我需要根據內容設置UiElement的高度。我將內容設置爲文本塊並設置文本塊MaxWidth是一些值,MaxHeight是double.MaxValue。現在我設置這個文本塊到孩子border.nowi測量像下面如何根據文本塊中的內容設置UIElement的動態高度

textBlock.MaxWidth=200; 
textBlock.MaxHeight=double.MaxValue; 
var area=new Border{child=textBlock}; 
area.Measure(new size(textBlock.MaxWidth,textBlock.MaxHeight)); 
var r=area.DesiredSize; 

但上面的代碼中給出了不同寬度的文本塊的不正確desiredsize邊界。有沒有其他方法可以根據文字內容計算高度?

回答

0

使用以下

<ViewBox Width="200"> 
<TexBlock Text="..." ... /> 
</ViewBox> 

它會適應的TextBlock的大小有始終是寬的矩形。

嘗試,並告訴我,如果我想你實際上是在尋找WCF控制你的情況

0

解決所謂的StackPanel,但我會提供的代碼改變高度編程以及。

使用StackPanel中,在XAML:

<StackPanel Width="200"> 
    <Border BorderThickness="1" BorderBrush="Black"> 
     <TextBlock TextWrapping="Wrap" Text="This TextBlock will be 
      wrapped by a black Border that has the height of the TextBlock 
      and with it's Width provided from the StackPanel"/> 
    </Border> 
</StackPanel> 

或更改TextBlock的高度編程:

<Border x:Name="border" BorderBrush="Black" BorderThickness="1" Height="100" Width="200"> 
    <TextBlock x:Name="textBlock" Background="Black" Width="40" Height="40"/> 
</Border> 

然後你的代碼很簡單:border.Height = textBlock.Height;

0
<Border Width="300" BorderThickness="1" BorderBrush="Black"> 
    <TextBlock Text="welcome" Foreground="Blue"/> 
    </Border> 
相關問題