1

i tried do some content in FlowDocument to be highlighten by rectangle. like following code:

<FlowDocument>
    <Paragraph>
        <Span>
            here is a span.
            <Span.Background>
                <VisualBrush>
                    <VisualBrush.Visual>
                        <Rectangle Fill="Gray" RadiusX="5" RadiusY="5" Width="100" Height="50"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Span.Background>
        </Span>
    </Paragraph>
<FlowDocument>

i want to set rectangle's width and height to span. how can i get span's actual width and height which determined by span's content's length?

added:

it dosen't work. (it occured System.InvalidOperationException in design time)

<FlowDocument>
    <Paragraph>
        <Span>
            Here is a span.
            <Span.Background>
                <VisualBrush>
                    <VisualBrush.Visual>
                        <Rectangle Fill="Gray" RadiusX="5" RadiusY="5" Height="50" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Span}}, Path=Width}"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Span.Background>
        </Span>
    </Paragraph>
</FlowDocument>
mjk6026
  • 1,484
  • 3
  • 20
  • 38

1 Answers1

1

Try something like this

Width="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type Span}},
Path=ActualWidth}"
>

A converter can be usefull on this binding for proportions, tell me if you need a converter example also.

Gab
  • 5,604
  • 6
  • 36
  • 52
  • @mjk6026 Path=ActualWidth, its not the same as Path=Width. Width/Height is the requested or layout size. ActualWidth/ActualHeight is the rendered size. See http://stackoverflow.com/questions/607827/what-is-the-difference-between-width-and-actualwidth-in-wpf for more details – Gab Aug 02 '11 at 18:18
  • no, no. that's a not point. it occured error when i modified width to actual width. first of all, span class doesn't have any property both of width and actualwidth. – mjk6026 Aug 02 '11 at 18:31