zoukankan      html  css  js  c++  java
  • WPF ViewBox中的TextBlock自适应

    想让 TextBlock即换行又能自动根据内容进行缩放,说到自动缩放,当然是ViewBox控件了,而TextBlock有TextWrapping属性控制换行,

    所以在ViewBox中套用一个TextBlock,然后设置换行,问题就能解决了,代码如下:

    <Window x:Class="ViewBoxAndTextblock.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Viewbox>
                <TextBlock x:Name="tb" Text="Die PRO-Version von Driver Easy wird automatisch einen Wiederherstellungspunkt erstellen. Sie ermöglicht es Ihnen, Ihr System auf einen vorherigen Punkt zurückzusetzen.Die manuelle Erstellung eines Wiederherstellungspunktes ist nicht einfach. Upgraden Sie auf die PRO-Version und Driver Easy wird automatisch den Wiederherstellungspunkt erstellen." TextWrapping="Wrap" />
            </Viewbox>
            <Button Click="Button_Click" Content="test" Margin="141.418,242.433,143,37"/>
        </Grid>
    </Window>

      结果呢?如下图:

       

      这是什么情况,TextBlock居然没有换行???

          后来经过Google和Stack Overflow,解决方法是要设置TextBlock的宽度,根据需求设置Width、或者MaxWidth (注:不能单独设MinWidth),这里设为 Width = 200, 结果显示如下:

          

          为了看的更清楚,这里设置一些ViewBox的Height,然后点击test按钮,TextBlock的Width增加20,结果如下:

         

          代码如下:

          XAML

    <Window x:Class="ViewBoxAndTextblock.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Viewbox Height="200" VerticalAlignment="Top">
                <TextBlock x:Name="tb" Text="Die PRO-Version von Driver Easy wird automatisch einen Wiederherstellungspunkt erstellen. Sie ermöglicht es Ihnen, Ihr System auf einen vorherigen Punkt zurückzusetzen.Die manuelle Erstellung eines Wiederherstellungspunktes ist nicht einfach. Upgraden Sie auf die PRO-Version und Driver Easy wird automatisch den Wiederherstellungspunkt erstellen." TextWrapping="Wrap" Width="200" Background="GreenYellow"/>
            </Viewbox>
            <Button Click="Button_Click" Content="test" Margin="141.418,242.433,143,37"/>
        </Grid>
    </Window>
    View Code

          后台代码:

      public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                tb.Width += 20;
                tb.Text += "FASDFEF";
            }
        }
    View Code
  • 相关阅读:
    Codeforces Round #249 (Div. 2) D. Special Grid 枚举
    图论二
    C语言中的atan和atan2(转)
    BestCoder Round #79 (div.2)
    数学
    LCA
    二分图
    动态规划
    线段树
    树状数组
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/6640398.html
Copyright © 2011-2022 走看看