WPF:如何在文本流中嵌入按钮(将文本环绕按钮)?

发布于 2024-08-02 04:17:13 字数 1065 浏览 6 评论 0原文

我想就以下问题提出建议:我想将 Button 嵌入到文本流中,但是当我嵌入 ButtonLabel 时> (或 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泼猴你往哪里跑 2024-08-09 04:17:13

要绑定到 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.

明月夜 2024-08-09 04:17:13

我发现正确实现 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.

寒尘 2024-08-09 04:17:13

解决方案: BindableRun 类 + 以下标记:

<TextBlock>
    <Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
    <common:BindableRun x:Name="Subject" BindableText="{Binding Path=Subject}"/>
</TextBlock>

Solution: BindableRun class + the following markup:

<TextBlock>
    <Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
    <common:BindableRun x:Name="Subject" BindableText="{Binding Path=Subject}"/>
</TextBlock>
爱人如己 2024-08-09 04:17:13

有趣的是,它适用于 UserControl 的设计者......
在这种情况下,使用控件的“属性更改”将值设置为“运行”就足够了。 我的意思是,如果您有类似的内容:

<TextBlock>          
   <Run Text="{Binding ElementName=thisCtrl, Path=Description}" />
</TextBlock>

那么只需命名运行,然后在 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:

<TextBlock>          
   <Run Text="{Binding ElementName=thisCtrl, Path=Description}" />
</TextBlock>

Then just name the run, and on your property change handler of your UserControl DependencyProperty get/set the value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文