简体   繁体   English

如何在Viewbox中设置Textblock的FontSize

[英]How to set FontSize of a Textblock, which is in a Viewbox

I can't set the FontSize of the Text in a TextBlock , since the TextBox is in a Viewbox . 由于TextBox位于Viewbox ,因此无法在TextBlock设置TextFontSize Why? 为什么?

<Grid Margin="35,30,35,0" ShowGridLines="False" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="270 px" Width="2*"/>
        <ColumnDefinition Width="5*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition  />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Border Grid.Row="0" Grid.Column="0" BorderBrush="Black" BorderThickness="1,1,1,0" />
    <Viewbox>
        <TextBlock  Margin="10,5,0,5" Grid.Row="0" Grid.Column="0">Team:</TextBlock>
    </Viewbox>
    <Border Grid.Row="1" Grid.Column="0" BorderBrush="Black" BorderThickness="1,1,1,0" />
    <TextBlock Margin="10,5,0,5" Grid.Row="1" Grid.Column="0">Beschreibung:</TextBlock>
    <Border Grid.Row="2" Grid.Column="0" BorderBrush="Black" BorderThickness="1,1,1,0" />
    <TextBlock Margin="10,5,0,5" Grid.Row="2" Grid.Column="0">Milestone:</TextBlock>
    <Border Grid.Row="3" Grid.Column="0" BorderBrush="Black" BorderThickness="1,1,1,1" />
    <TextBlock Margin="10,5,0,5" Grid.Row="3" Grid.Column="0">Status:</TextBlock>
    <Border Grid.Row="0" Grid.Column="1" BorderBrush="Black" BorderThickness="0,1,1,0" />
    <TextBlock x:Name="tb_Team_dyn" Margin="10,5,0,5"  Grid.Row="0" Grid.Column="1"
               FontWeight="Bold" Text="{Binding Data.Team}" TextWrapping="Wrap"></TextBlock>
    <Border Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0,1,1,0" />
    <TextBlock x:Name="tb_Descr_dyn" Margin="10,5,0,5" Grid.Row="1" Grid.Column="1"
               FontWeight="Bold" Text="{Binding Data.Description}" TextWrapping="Wrap"/>
    <Border Grid.Row="2" Grid.Column="1" BorderBrush="Black" BorderThickness="0,1,1,0" />
    <TextBlock x:Name="tb_Milestone_dyn" Margin="10,5,0,5" Grid.Row="2" Grid.Column="1"
               FontWeight="Bold" Text="{Binding Data.Milestone}" TextWrapping="Wrap"/>
    <Border Grid.Row="3" Grid.Column="1" BorderBrush="Black" BorderThickness="0,1,1,1" />
    <StackPanel Margin="10,5,0,5" Grid.Row="3" Grid.Column="1" Orientation="Horizontal">
        <Image  x:Name="imgSmile" MaxWidth="38" Source="{Binding Data.Smiley}" />
        <TextBlock x:Name="tb_Status_dyn" Margin="{Binding Data.SmileyMargin}"
                   Foreground="{Binding Data.Col}" FontWeight="Bold"
                   Text="{Binding Data.Status}" TextWrapping="Wrap"></TextBlock>
    </StackPanel>
</Grid>

I can set the FontSize s of all of the TextBlock s but not of the TextBlock which is in the Viewbox . 我可以设置FontSize所有的第TextBlock秒,但不是的TextBlock这是在Viewbox

Keep in mind that the FontSize sets the actual size of the text but the Viewbox then scales it after the text is laid out and rendered. 请记住,FontSize设置了文本的实际大小,但是Viewbox在布局和渲染文本后会对其进行缩放。 You will see the same effect if you place an element with a fixed Width and Height in a Viewbox. 如果将具有固定的Width和Height的元素放置在Viewbox中,您将看到相同的效果。 The Viewbox doesn't change the Width, Height, or FontSize properties themselves but the final rendering appears different. Viewbox本身不会更改Width,Height或FontSize属性,但最终呈现效果会有所不同。

What you're actually looking for is an initial layout for the Viewbox itself that will make its initial size equal to that of the TextBlock, which will effectively set the scaling of the Viewbox to 1:1. 您实际上要查找的是Viewbox本身的初始布局,该布局将使其初始大小等于TextBlock的初始大小,这将有效地将Viewbox的缩放比例设置为1:1。 You can achieve this in a number of ways depending on your application (directly in code, Bindings with ValueConverters, etc) but the basic method would be to measure the initial size of a parent element and set appropriate Margins to scale down the Viewbox within the layout area assigned to it. 您可以根据自己的应用程序以多种方式实现此目标(直接在代码中,使用ValueConverters进行绑定等),但基本方法是测量父元素的初始大小并设置适当的边距以缩小视图中的Viewbox。分配给它的布局区域。 This would then allow the Viewbox to change size along with its parent as it maintains the Margins that you set. 然后,这将允许Viewbox更改其父项的大小,因为它会保留您设置的边距。 Also look at the StretchDirection, MinHeight, and MinWidth on the Viewbox. 还要在Viewbox上查看StretchDirection,MinHeight和MinWidth。

Some sample code would be helpful. 一些示例代码会有所帮助。 Off the top of my head... is the textbox set to multiline? 在我的头顶上方...文本框是否设置为多行? If not the inner text will probably stretch along with the textbox. 否则,内部文本可能会与文本框一起延伸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM