0

我正在尝试将 RadioButtons 嵌入 RichTextBox 段落中,但未应用 RadioButton 的宽度和高度值:

<Style x:Key="styleRb" TargetType="{x:Type RadioButton}">
    <Setter Property="Focusable" Value="False"></Setter>
    <Setter Property="Margin" Value="3,0,3,-1"></Setter>
    <Setter Property="Padding" Value="0,0,0,0"></Setter>
    <Setter Property="HorizontalAlignment" Value="Center"></Setter>
    <Setter Property="VerticalAlignment" Value="Center"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RadioButton}">
                <BulletDecorator Background="Transparent">
                    <BulletDecorator.Bullet>
                        <!--Values are applied:-->
                        <!--<Grid Width="13" Height="13">-->
                        <!--Values are not applied:-->
                        <Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Paragraph}}, 
                                              Path=FontSize}"
                              Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Paragraph}}, 
                                               Path=FontSize}">
                            <Ellipse Stroke="Black" StrokeThickness="2.0"
                                     Fill="Gold" Opacity="1.0">
                            </Ellipse>
                        </Grid>
                    </BulletDecorator.Bullet>
                </BulletDecorator>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

嵌入代码是这样的:

RadioButton rb = new RadioButton();
rb.Style = (Style)Application.Current.FindResource("styleRb");
InlineUIContainer container = new InlineUIContainer(rb, rtbInsertionPosition);
4

1 回答 1

2

段落元素实际上并不参与可视化树。因此 Binding 在树中找不到 Paragraph 类型的元素。这就是绑定失败的原因。代替 Paragraph,另一个名为ParagraphVisual的控件用于代替 Paragraph 进行呈现。您可以使用 Snoop 轻松找到它。

在此处输入图像描述

段落标签只是承载文本的占位符。ParagraphVisual 是用于绘制文本的内部类,因此不能在 XAML 中使用该类型。

http://referencesource.microsoft.com/#PresentationFramework/Framework/MS/Internal/PtsHost/ParagraphVisual.cs

于 2014-12-03T15:21:10.780 回答