【发布时间】:2013-04-18 23:00:10
【问题描述】:
我有一个有趣的问题一直在尝试解决。基本上我有一个使用WrapPanel 的项目控件,因为它是ItemsPanel 来模拟从几个绑定字符串构建的段落。但是,有时我需要强制中断,例如当我开始一个新段落时,但是在 TextBlock DateTemplate 中插入中断实际上并没有在父换行面板中插入中断。代码如下:
<ItemsControl ItemsSource="{Binding Fragments}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock
TextWrapping="Wrap"
Text="{Binding}"/> <!--If this text has a break it won't
propagate that break to the wrap panel,
but instead just in this text block which
causes the formatting to look wrong-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
下面是片段的简单定义,将显示我在说什么:
Fragments = new ObservableCollection<string>();
Fragments.Add("This is");
Fragments.Add("the first line, ");
Fragments.Add("it is very long and will drift to the ");
Fragments.Add("second line naturally since it is controlled by a wrap panel");
Fragments.Add("\n\r This I want to force to the line below where the line above ends");
Fragments.Add("rapid \n\r new \n\r lines");
我希望它作为段落继续连接,但在遇到手动中断时尊重它们。像这样:
这是第一行,很长,会飘到第二行 自然是因为它是由一个环绕面板控制的。 我想强制到上面的线结束的下面的线。 迅速的 新的 行【问题讨论】:
-
你有什么理由不连接字符串然后将其绑定到一个简单的文本框?
-
你有什么理由不连接字符串然后将其绑定到一个简单的文本框?
-
“Break”是指“NewLine”吗?
-
@sa_ddam213 是换行符。
-
真的,在这里工作正常,你能张贴你期望它的样子的图片吗,我想我不明白你在做什么,你能不能只使用
StackPanel作为ItemsPanel或仅使用TextBox并填充Lines属性而不是使用ItemsControl?
标签: c# wpf xaml itemscontrol wrappanel