3

DrawingVisualオブジェクトがあり、その塗りと線を変更したいと考えています。

私は塗りつぶしのためにこれを試しました:

DrawingVisual MyDrawing = new DrawingVisual();
SetFill(Brushes.Red, MyDrawing.Drawing);

SetFill の場所:

private void SetFill(Brush fill, DrawingGroup group)
{
    foreach (Drawing drw in group.Children)
    {
        if (drw is DrawingGroup)
            SetFill(fill, drw as DrawingGroup);
        else if (drw is GeometryDrawing)
        {
            GeometryDrawing geo = drw as GeometryDrawing;
            geo.Brush = fill;

            using (DrawingContext context = MyDrawing.RenderOpen())
            {
                context.DrawDrawing(group);
            }
        }
    }
}

しかし、この方法では、(MyDrawing に) 変換が適用されていないかのように、私の DrawingVisual が別の位置に描画されることがあります。

また、この命令を変更すると、次のようになります: 奇妙な効果が得られますcontext.DrawDrawing(group); : context.DrawDrawing(MyDrawing.Drawing); Fill を変更すると、最初は何も起こりませんが、2 回目は Figure の位置を変更せずに Fill が正しく変更されます。

どのようにできるのか?

4

3 に答える 3

2

問題へのはるかに簡単なアプローチ(塗りつぶしを動的に変更する)は、すべての塗りつぶしに独自のSolidColorBrushを使用し、必要に応じてその色を変更することです。

于 2012-05-09T08:06:32.340 に答える
2

ビジュアルを再描画する必要はありません。これはうまくいくはずです。

private void SetFill(Brush fill, DrawingGroup group)
{
    foreach (Drawing drw in group.Children)
    {
        if (drw is DrawingGroup)
            SetFill(fill, drw as DrawingGroup);
        else if (drw is GeometryDrawing)
        {
            GeometryDrawing geo = drw as GeometryDrawing;
            geo.Brush = fill; // For changing FILL

            // We have to set Pen properties individually.
            // It does not work if we assign the whole "Pen" instance.
            geo.Pen.Brush = fill; 
            geo.Pen.Thickness = 1.0;
        }
    }
}
于 2013-05-20T00:31:11.490 に答える
1

おそらくあなたはすでにこの問題を解決していますが、これがおそらく同じことの私の解決策です。私は独自の DrawingVisual を持っています。これは新しく、あまりテストされていないコードですが、今のところ非常にうまく機能します。

public class MyDrawingVisual : DrawingVisual
{
    private bool myIsSelected = false;

    public VillageInfo VillageInfo { get; set; }

    public bool IsSelected 
    {
        get { return myIsSelected; }
        set
        {
            if (myIsSelected != value)
            {
                myIsSelected = value;

                // Retrieve the DrawingContext in order to create new drawing content.
                DrawingContext drawingContext = this.RenderOpen();

                // Create a rectangle and draw it in the DrawingContext.
                Rect rect = new Rect(new System.Windows.Point(VillageInfo.X + 0.1, VillageInfo.Y + 0.1), new System.Windows.Size(0.9, 0.9));
                drawingContext.DrawRectangle(new SolidColorBrush(myIsSelected ? Colors.White : VillageInfo.Color), (System.Windows.Media.Pen)null, rect);

                // Persist the drawing content.
                drawingContext.Close();
            }
        }
    }
}
于 2012-08-20T07:48:06.517 に答える