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
  • 相关阅读:
    [UE4]蓝图中清空变量值或设置为null
    [UE4]运行时脱离视角,进入自由视角
    [UE4]扔枪
    [UE4]反射
    为帮助保护你的安全,您的Web浏览器已经限制此文件显示可能访问您的计算机的活动内容
    [UE4]根据名字调用函数(蓝图)
    [UE4]移除UI(User Widget)并销毁
    [UE4]Return Node节点好用法
    [UE4]关于分支Sequence和条件分支的组合用法
    [UE4]隐藏对象Set Visibility
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/6640398.html
Copyright © 2011-2022 走看看