15

WPFでは、要素に波状の下線(Wordのスペルミスなど)を追加する簡単な方法はありFlowDocumentますか?クラスはありますがUnderline、スタイルを設定する方法がないようです。

4

5 に答える 5

30

Robert Macne のソリューションに次の変更を加えることで、波状の効果を作成できます。

Grid.Resources セクションにビジュアル ブラシを追加します。

<VisualBrush x:Key="WavyBrush" Viewbox="0,0,3,2" ViewboxUnits="Absolute" Viewport="0,0.8,6,4" ViewportUnits="Absolute" TileMode="Tile">
    <VisualBrush.Visual>
        <Path Data="M 0,1 C 1,0 2,2 3,1" Stroke="Red" StrokeThickness="0.2" StrokeEndLineCap="Square" StrokeStartLineCap="Square" />
    </VisualBrush.Visual>
</VisualBrush>

ペンを次のように変更します。

<Pen Brush="{StaticResource WavyBrush}" Thickness="6" />
于 2009-03-11T07:58:36.863 に答える
6

赤い下線はとてもシンプルです:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <FlowDocument x:Key="doc">
            <Paragraph>
                <Run Text="This text is underlined in red.">
                    <Run.TextDecorations>
                        <TextDecoration Location="Underline">
                            <TextDecoration.Pen>
                                <Pen Brush="Red" Thickness="1" DashStyle="{x:Static DashStyles.Dot}"/>
                            </TextDecoration.Pen>
                        </TextDecoration>
                    </Run.TextDecorations>
                </Run>
            </Paragraph>
        </FlowDocument>
    </Grid.Resources>
    <FlowDocumentReader Document="{StaticResource doc}"/>
</Grid>

波状の赤い下線はもう少し複雑ですが、波状の赤いものを含む VisualBrush を作成し、それを下線の TextDecoration に指定するペンのブラシとして設定できると思います。 編集: これについてはbstoneyの投稿を参照してください。

于 2009-03-10T22:10:30.473 に答える
1

これは古い質問だと思いますが、私はこのブラシが好きです。少し角張っていますが、とてもきれいです。

    <VisualBrush x:Key="WavyBrush">
        <VisualBrush.Visual>
            <Path Data="M 0,2 L 2,0 4,2 6,0 8,2 10,0 12,2" Stroke="Red"/>
        </VisualBrush.Visual>
    </VisualBrush>
于 2014-06-13T06:48:21.803 に答える
0

別の方法は次のとおりです。2 つの線 (逆 V のようなもの) で描画グループを作成し、その描画グループを描画ブラシのコンテンツとして渡し、Tile をモードにして繰り返しできるようにします。しかし、ここでは 2 本の線を描いているので、頂点が尖っています。

サンプルは次のようになります。 ここに画像の説明を入力

        // Configuring brush.
        DrawingGroup dg = new DrawingGroup();
        using (DrawingContext dc = dg.Open())
        {
            Pen pen = new Pen(Brushes.Red, 1);
            dc.DrawLine(pen, new System.Windows.Point(0.0, 0.0), new System.Windows.Point(3.0, 3.0));
            dc.DrawLine(pen, new System.Windows.Point(3.0, 3.0), new System.Windows.Point(5.0, 0.0));
        }
        public DrawingBrush brush = new DrawingBrush(dg);
        brush.TileMode = TileMode.Tile;
        brush.Viewport = new Rect(0, 0, 4, 4);
        brush.ViewportUnits = BrushMappingMode.Absolute;
        
        // Drawing line on VisualCollection
        private VisualCollection visualCollection = new VisualCollection(this);
        DrawingVisual dv = new DrawingVisual();
        using (DrawingContext drawingContext = dv.RenderOpen())
        {
            drawingContext.DrawLine(new Pen(brush, 2), new Point(50, 100), new Point(200, 100));
            base.OnRender(drawingContext);
        }
        this.visualCollection.Add(dv);
于 2022-01-12T10:33:17.303 に答える