2012-08-03 37 views
4

我正在Silverlight中爲嵌入式窗口工作,並且希望將文本放到TextBlock上,如圖片所示,我希望textBlock適合文本(我想刪除附加圖片中的黃色空間)如何使TextBlock大小適合文本大小

Pic

有人可以幫助我?

最好的問候, 盧卡

這是我目前使用的XAML:

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
x:Class="FullTest.PUIsocUI" 
d:DesignWidth="480" d:DesignHeight="272"> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <StackPanel Orientation="Horizontal"  VerticalAlignment="Bottom" Height="64" Grid.Row="1"> 
     <RadioButton x:Name="PowerMeasurement"  GroupName="PowerTabControls" IsChecked="True" Checked="PowerMeasurement_Checked" Unchecked="PowerMeasurement_Unchecked" Content="POWER" BorderThickness="0"/> 
     <RadioButton x:Name="PowerMode"    GroupName="PowerTabControls" Checked="PowerMode_Checked" Unchecked="PowerMode_Unchecked"/>  
     <RadioButton x:Name="PowerLimit"   GroupName="PowerTabControls" IsChecked="False" Click="PowerLimit_Click" Checked="PowerLimit_Checked" Unchecked="PowerLimit_Unchecked"/>  
    </StackPanel> 
    <Grid> 
     <Grid x:Name="PowerMeasurementPage" Margin="0,0,0,64" > 
     <!-- tab page za meritev--> 
      <TextBlock Text="POWER" Style="{StaticResource FunctionNameTextBlockStyle}" HorizontalAlignment="Left" VerticalAlignment="Top"/> 
      <TextBlock Text="11.555" Style="{StaticResource ResultNameTextBlockStyle}" /> 
     </Grid> 
     <Grid x:Name="PowerModePage" Margin="0,0,0,64"> 
     <!-- tab page za Mode--> 
     </Grid> 
     <Grid x:Name="PowerLimitPage" Margin="0,0,0,64"> 
     <!-- tab page za Limita--> 
     </Grid> 
    </Grid> 
</Grid> 

<Style TargetType="TextBlock" x:Key="FunctionNameTextBlockStyle"> 
     <Setter Property="FontFamily" Value="ALTERNATE_GOTHIC#AlternateGothic2 BT"/> 
     <Setter Property="FontSize" Value="44"/> 
     <Setter Property="Margin" Value="57,27,0,0"/> 
    </Style> 

這是我想 - >

pic2

+2

您的目標只是爲了使文本項目的對齊更容易?如果是這樣,只需使用負頂部邊距。問題在於文本塊使用字體本身定義的填充來確定TextBlock的大小。 – 2012-08-03 10:29:57

+0

是的,這正是我想要的,使對齊更容易:) – 2012-08-03 13:33:46

+0

我可以得到你想要的效果,在TextBlock本身使用邊距,但在樣式設置器的負邊緣似乎沒有效果。 – 2012-08-03 15:16:46

回答

0

另一種方式更簡單。你只需要TextBlock自己調整大小,它會根據文本安排自己。你應該看看它的容器可能(可能粘貼一些代碼?)。嘗試設置TextBlock這樣的:

<TextBlock Text="POWER" HorizontalAlignment="Center" VerticalAlignment="Center" /> 

UPDATE直視VS,對我來說,似乎它只是一種字體預留了空間。不知道你如何避免這種情況。

+0

嗨, 我使用Silverlight的嵌入式系統,它不支持ViewBox – 2012-08-03 08:27:24

+0

而關鍵點是*「我希望textBlock適合文本」*而不是其他方式。 – 2012-08-03 08:28:06

+0

@HiTechMagic,看到更新 – Snowbear 2012-08-03 08:39:29

1
<TextBlock Text="POWER" FontSize="44" FontWeight="SemiBold" RenderTransformOrigin="0.5,0.5"> 
    <TextBlock.RenderTransform> 
    <CompositeTransform ScaleY="2"/> 
    </TextBlock.RenderTransform> 
</TextBlock> 

在我提供的示例中,請參閱ScaleY聲明?調整該值以滿足您的需求。這是在不使用ViewBox的情況下實現該效果的一種方法。另一個可能是將該TextBlock轉換爲路徑並根據需要進行調整,但它必須是靜態標籤纔能有用。

希望這會有所幫助! :)

+0

+1:非常有創意的解決方案。字體大小需要調整,但這將避免字體填充問題。尼斯。 – 2012-08-03 15:10:02

+0

@HiTech魔術師 - 謝謝,有時候我們必須和你一起做檸檬水。 :) – 2012-08-03 15:25:52

+0

這可能是WP或silvelight 4,5的解決方案,但不適用於嵌入式Windows的silverlight,這就是我所需要的。 – 2012-08-06 08:07:34

0

在VS2015中,您可以使用TextLineBounds屬性(在您的情況下將其設置爲Tight)輕鬆完成此操作。我在UAP中使用它,不確定它在Silverlight中可用。但是它會被截斷爲小寫字母,如g,q等,因爲發佈在這裏:TextLineBounds snipping bottom of letters

相關問題