【问题标题】:WPF: How to embed button inside text flow (wrap text around button)?WPF:如何在文本流中嵌入按钮(在按钮周围环绕文本)?
【发布时间】:2009-08-15 22:51:57
【问题描述】:

我想对以下问题提出建议:我想将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>

【问题讨论】:

    标签: wpf button flowdocument word-wrap wrappanel


    【解决方案1】:

    要绑定到 Run.Text,请查看 Fortes 的 BindableRun 类。易于实施,我在所有项目中都使用它。

    【讨论】:

    • 大声笑,我在 5 分钟前发现了这个,并用它实现了一个解决方案:))。无论如何谢谢:)。
    • 我发现正确实现 BindableRun 非常棘手 - 当绑定内容从 null 变为非 null 时,您的(以及几乎所有其他可用的实现)将导致 wpf 布局引擎异常。微软的正确实现在这里 - 但真的很棘手msdn.microsoft.com/en-us/magazine/dd569761.aspx
    【解决方案2】:

    我发现正确实现 BindableRun 非常棘手 - 当绑定的内容从 null 变为非 null 时,几乎所有其他可用的实现都会导致 wpf 布局引擎异常 - 请参阅this problem, keyword "Collection was modified; enumeration operation may not execute."

    Microsoft 的正确实现是 here - 它显示了这确实是多么棘手。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        有趣的是,它适用于 UserControl 的设计器... 在这种情况下,使用控件的 Property Change 将值设置为 Run 就足够了。我的意思是,如果你有类似的东西:

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

        然后只需命名运行,并在您的 UserControl DependencyProperty 的属性更改处理程序上获取/设置值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-20
          相关资源
          最近更新 更多