简体   繁体   中英

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 . 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 .

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. You will see the same effect if you place an element with a fixed Width and Height in a Viewbox. The Viewbox doesn't change the Width, Height, or FontSize properties themselves but the final rendering appears different.

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. 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. This would then allow the Viewbox to change size along with its parent as it maintains the Margins that you set. Also look at the StretchDirection, MinHeight, and MinWidth on the Viewbox.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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