这是我最接近的:
<UserControl x:Class="FlowPanelTest.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls" Width="250" Height="300"> <Border BorderBrush="Black" BorderThickness="2" > <Controls:WrapPanel> <TextBlock x:Name="tb1" TextWrapping="Wrap">Short text. </TextBlock> <TextBlock x:Name="tb2" TextWrapping="Wrap">A bit of text. </TextBlock> <TextBlock x:Name="tb3" TextWrapping="Wrap">About half of a line of text.</TextBlock> <TextBlock x:Name="tb4" TextWrapping="Wrap">More than half a line of longer text.</TextBlock> <TextBlock x:Name="tb5" TextWrapping="Wrap">More than one line of text,so it will wrap onto the following line.</TextBlock> </Controls:WrapPanel> </Border> </UserControl>
但问题在于虽然文本块tb1和tb2将会进入同一行,因为它们有足够的空间完全存在,但是tb3以后不会在前一个块的同一行开始,即使它将包裹到下一行.
我希望每个文本块在同一行上从前一个文本块结束.我想在某些文本上放置click事件处理程序.我还想要段落休息.基本上我正在努力解决Silverlight 2.0的XAML子集中缺少FlowDocument和Hyperlink控件的问题.
回答答案中提出的问题:
为什么不使用不可点击文本的运行?如果我只在可点击的文本上使用单独的TextBlocks,那么这些文本仍然会受到上面说明的包装问题的影响.而TextBlock就在链接之前,而TextBlock就在之后.基本上所有这一切.看起来我没有很多机会在同一个TextBlock中放置多个运行.
使用RegExs和循环将链接与其他文本分开根本不是问题,问题是显示布局.
为什么不将每个单词放在WrapPanel中的单个TextBlock中除了是一个丑陋的黑客之外,这对于换行没有好处 – 布局不正确.
它还会使链接文本的下划线样式变成虚线.
这是一个示例,其中每个单词都在自己的TextBlock中.尝试运行它,请注意,换行符根本没有显示在正确的位置.
<UserControl x:Class="SilverlightApplication2.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls" Width="300" Height="300"> <Controls:WrapPanel> <TextBlock TextWrapping="Wrap">Short1 </TextBlock> <TextBlock TextWrapping="Wrap">Longer1 </TextBlock> <TextBlock TextWrapping="Wrap">Longerest1 </TextBlock> <TextBlock TextWrapping="Wrap"> <Run>Break</Run> <LineBreak></LineBreak> </TextBlock> <TextBlock TextWrapping="Wrap">Short2</TextBlock> <TextBlock TextWrapping="Wrap">Longer2</TextBlock> <TextBlock TextWrapping="Wrap">Longerest2</TextBlock> <TextBlock TextWrapping="Wrap">Short3</TextBlock> <TextBlock TextWrapping="Wrap">Longer3</TextBlock> <TextBlock TextWrapping="Wrap">Longerest3</TextBlock> </Controls:WrapPanel> </UserControl>
那么LinkLabelControl如here和here.它有与上述方法相同的问题,因为它大致相同.尝试运行示例,并使链接文本越来越长,直到它换行.请注意,链接从新行开始,它不应该.使链接文本更长,以使链接文本长于一行.请注意,它根本不会包装,它会切断.此控件也不处理换行符和段落中断.
为什么不将文本全部放入运行中,检测包含TextBlock的点击并确定单击了哪个运行
运行没有鼠标事件,但包含TextBlock.我找不到一种方法来检查运行是否在鼠标下(SilverLight中不存在IsMouSEOver)或查找运行的边界几何(无剪辑属性).
有VisualTreeHelper.FindElementsInHostCoordinates()
下面的代码使用VisualTreeHelper.FindElementsInHostCoordinates来获取单击下的控件.输出列出了TextBlock而不是Run,因为Run不是UiElement.
private void theText_MouseLeftButtonDown(object sender,System.Windows.Input.MouseButtonEventArgs e) { // get the elements under the click UIElement uiElementSender = sender as UIElement; Point clickPos = e.GetPosition(uiElementSender); var UiElementsUnderClick = VisualTreeHelper.FindElementsInHostCoordinates(clickPos,uiElementSender); // show the controls string outputText = ""; foreach (var uiElement in UiElementsUnderClick) { outputText += uiElement.GetType().ToString() + "\n"; } this.outText.Text = outputText; }
使用带有边距的空文本块将内容空间放到下一行
我还在考虑这个.
如何计算换行块的正确宽度以强制将内容强制到下一行?太短,以下内容仍然在右边的同一行.太长,“linebreak”将在以下行,其后面的内容.调整控件大小时,您必须调整中断的大小.
一些代码是:
TextBlock lineBreak = new TextBlock(); lineBreak.TextWrapping = TextWrapping.Wrap; lineBreak.Text = " "; // need adaptive width lineBreak.Margin = new Thickness(0,200,0);
解决方法
使用运行来连接所有不会发生事件的值,然后将那些有事件的值分解为自己的文本块,冲洗重复.
在我看来,你应该能够使用RegEx和一些循环来做到这一点.
查看Jesse Liberty在包装面板上的帖子,看看是否有任何想法.
http://silverlight.net/blogs/jesseliberty/archive/2008/12/03/the-wrap-panel.aspx
心连心