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
  • 相关阅读:
    【Java技术系列】爱情36技之暗送秋波的技术
    【Java技术系列】爱情36技之追美妹的技术
    设计模式(五)——建造者模式
    设计模式(四)——一文搞清楚三种工厂模式
    设计模式(三)—— 工厂方法模式
    设计模式(二)—— 简单工厂模式
    设计模式实战——开发中常用到的单例模式
    设计模式(一)—— 单例模式
    设计模式(开篇)—— 设计模式概述
    设计模式系列博客传送门
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/6640398.html
Copyright © 2011-2022 走看看