• 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
  • 相关阅读:
    python读取二进制文件写入到txt
    python格式化输出
    字符编码
    python--随时记录
    python-web服务器
    openssh移植
    select、poll、epoll
    (总结)Nginx/LVS/HAProxy负载均衡软件的优缺点详解
    heartbeat与keepalived的区别
    salt 常用命令整理
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/6640398.html
Copyright © 2020-2023  润新知