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="" Width="">
<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>
后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
tb.Width += 20;
tb.Text += "FASDFEF";
}
}
WPF ViewBox中的TextBlock自适应的更多相关文章
- WPF/Silverlight中的RichTextBox总结
WPF/Silverlight中的RichTextBox总结 在WPF或者是在Silverlight中有个非常强大的可以编辑的容器控件RichTextBox,有的时间会采取该控件来作为编辑控件.鉴 ...
- 浅谈WPF本质中的数据和行为
WPF缩写为Windows Presentation Foundation的缩写,本文所要谈的就是WPF本质中的数据和行为,希望通过本文能对大家了解WPF本质有所帮助. 如果自己来做一个UI框架,我们 ...
- WPF,SilverLight中直线的样式示例
原文:WPF,SilverLight中直线的样式示例 XAML代码:// LineStyle.xaml<Viewbox Width="600" Height="50 ...
- 【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?
原文:[msdn wpf forum翻译]TextBlock等类型的默认样式(implicit style)为何有时不起作用? 原文链接:http://social.msdn.microsoft.co ...
- 如何追踪 WPF 程序中当前获得键盘焦点的元素并显示出来
原文:如何追踪 WPF 程序中当前获得键盘焦点的元素并显示出来 title: "如何追踪 WPF 程序中当前获得键盘焦点的元素并显示出来" publishDate: 2019-06 ...
- webrtc中的带宽自适应算法
转自:http://www.xuebuyuan.com/1248366.html webrtc中的带宽自适应算法分为两种: 1, 发端带宽控制, 原理是由rtcp中的丢包统计来动态的增加或减少带宽,在 ...
- winform中dataGridView高度自适应填充完数据的高度
// winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...
- WPFの布局中Panel的选用
一.Canvas 这个容器能够对元素做准确的定位,但同时也是其创建的页面不够灵活. 二.StackPanel 最大的优点是:他会顺序的对他的子元素进行排列显示.(没有任何附加属性) 要注意的是:他有两 ...
- 在WPF程序中使用摄像头兼谈如何使用AForge.NET控件(转)
前言: AForge.NET 是用C#写的一个关于计算机视觉和人工智能领域的框架,它包括图像处理.神经网络.遗传算法和机器学习等.在C#程序中使用摄像头,我习惯性使用AForge.NET提供的类库.本 ...
随机推荐
- 阿里云服务器 OSS的使用限制
使用限制 更新时间:2019-02-12 16:50:27 编辑 · OSS的使用限制及性能指标如下: 限制项 说明 归档存储 已经存储的数据从冷冻状态恢复到可读取状态需要 1 分钟的等待时间. ...
- Eclipse怎么全局搜索和替换(整个项目)
我们用Eclipse编程,有时候需要将整个项目的某个字符串替换成其他的.那么我们该怎么操作呢?请接着往下看! 一,我们首先打开Eclipse,单击要替换字符串的项目 二,按下组合键:ctrl + H, ...
- POJ2594 Treasure Exploration(最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8550 Accepted: 3 ...
- poj 2155 区间更新 单点查询
Matrix Time Limit: 3000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class ...
- Android-Kotlin-set/get方法的使用
Student.kt package cn.kotlin.kotlin_oop04 open class Person { open var personName:String = "我是父 ...
- [Leet code 2]Two Sum
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- oracle 字符串分割函数
首先创建一个类型: CREATE OR REPLACE TYPE str_split IS TABLE OF VARCHAR2 (4000); 创建函数: CREATE OR REPLACE FUNC ...
- WPF 打印界面(控件)到A4纸
这次遇到一个需求,就是将整个界面打印在A4纸上. 需求清楚后,Bing一下关于打印,就找到一个类PrintDialog ,其中两个方法可能会用到: 特别是public void PrintVisual ...
- .NET Entity Framework (with Oracle ODP.NET) -Code First
上一篇文章介绍了.NET Entity Framework ,并演示了Model First模式,本文将继续讨论 Code First 模式的实现. 一.摘要 1.目标 本文验证了通过Oracle D ...
- 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑
[源码下载] 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) InkCanv ...