2

我想对以下问题提出建议:我想将 a 嵌入Button到文本流中,但是当我将 a Buttonand Label(or TextBlock)嵌入到 中时WrapPanel,我得到了第一个数字:

替代文字 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>
4

4 回答 4

1

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

于 2009-08-16T00:42:07.363 回答
1

我发现正确实现 BindableRun 非常棘手 - 当绑定的内容从 null 更改为非 null 时,几乎所有其他可用的实现都会导致 wpf 布局引擎异常 - 请参阅此问题,关键字“集合已修改;枚举操作可能不执行。”

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

于 2009-11-30T00:46:30.427 回答
0

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

<TextBlock>
    <Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
    <common:BindableRun x:Name="Subject" BindableText="{Binding Path=Subject}"/>
</TextBlock>
于 2009-08-16T00:49:28.207 回答
0

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

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

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

于 2010-12-17T18:07:06.997 回答