WPF:如何在文本流中嵌入按钮(将文本环绕按钮)?
我想就以下问题提出建议:我想将 Button
嵌入到文本流中,但是当我嵌入 Button
和 Label
时> (或 TextBlock
)进入 WrapPanel
,我得到第一个数字:
alt text http://sklad.tomaskafka.com/files/wpf-wrappanel-problem.png
我认为解决方案之一可能是 FlowDocument,但我觉得对于像这样简单的控件(可以在数百个实例中使用)来说这太重了。 关于如何实现这一点,您还有其他想法吗? 谢谢你!
编辑: 一种解决方案可能如下(我不知道可以将更多内容放入 TextBlock
中),但我会失去绑定的能力(我需要):
<TextBlock TextWrapping="Wrap">
<Span>
<Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
<Run x:Name="MyLabel" Text="{Binding Path=Subject}" />
<!--
Problem: binding makes following error:
A 'Binding' cannot be set on the 'Text' property of type 'Run'.
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
-->
</Span>
</TextBlock>
I'd like an advice to the following problem: I want to embed a Button
into a text flow, but when I embed a Button
and Label
(or TextBlock
) into the WrapPanel
, I get the first figure:
alt text http://sklad.tomaskafka.com/files/wpf-wrappanel-problem.png
I think that one of solutions could be FlowDocument
, but I feel that this is far too heavy for a control simple like this (which could be used in several hundred instances). Do you have some other ideas about how to implement this? Thank you!
EDIT:
One solution could be the following (I didn't know it was possible to put more stuff into TextBlock
), but I would lose the ability to bind (which I need):
<TextBlock TextWrapping="Wrap">
<Span>
<Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
<Run x:Name="MyLabel" Text="{Binding Path=Subject}" />
<!--
Problem: binding makes following error:
A 'Binding' cannot be set on the 'Text' property of type 'Run'.
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
-->
</Span>
</TextBlock>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要绑定到 Run.Text,请查看 Fortes 的 BindableRun 类。 实施起来很简单,我在我的所有项目中都使用它。
To bind to Run.Text, checkout the BindableRun class by Fortes. Simple to implement, I use it all over my projects.
我发现正确实现 BindableRun 非常棘手 - 当绑定内容从 null 更改为非 null 时,几乎所有其他可用的实现都会导致 wpf 布局引擎出现异常 - 请参阅 此问题,关键字“集合已修改;枚举操作可能无法执行。”< /a>
Microsoft 的正确实现是此处 - 它显示了多么棘手这确实是。
I found that implementing BindableRun correctly is pretty tricky - and almost all other available implementations will cause an exception from wpf layouting engine when the bound content changes from null to something non-null - see this problem, keyword "Collection was modified; enumeration operation may not execute."
Corrrect implementation from Microsoft is here - it shows how tricky this really is.
解决方案: BindableRun 类 + 以下标记:
Solution: BindableRun class + the following markup:
有趣的是,它适用于 UserControl 的设计者......
在这种情况下,使用控件的“属性更改”将值设置为“运行”就足够了。 我的意思是,如果您有类似的内容:
那么只需命名运行,然后在 UserControl DependencyProperty 的属性更改处理程序上获取/设置值。
Funny thing it works on the designer of a UserControl...
In that case, using the Property Change of your control to set the value to the Run is enough. I mean, if you had something like:
Then just name the run, and on your property change handler of your UserControl DependencyProperty get/set the value.