سؤال

In my wpf application i am drawing a lot of geometries as below. My requirement is to change the color of drawingvisual with out redrawing it? any possibilities in wpf?

  using (DrawingContext vDrawingContext = vDrawingVisual.RenderOpen())
        {
          StreamGeometry vGeom = GetCutGeometry(mLength, mWidth);
          vDrawingContext.DrawGeometry(mBackGroundBrush, ForeGroundPen, vGeom);
          vDrawingContext.Close();
          VisualChildren.Add(vDrawingVisual);    

        }

How could be mBackGroundBrush dyamic colors?

هل كانت مفيدة؟

المحلول

Provided that mBackGroundBrush is a modifiable SolidColorBrush (i.e. it is created in your application and none of the predefined brushes), you could simply change its Color property. That will change the fill color of each drawn geometry with redrawing.

private SolidColorBrush mBackGroundBrush = new SolidColorBrush(Colors.Black);

...

mBackGroundBrush.Color = Colors.Red;

or

mBackGroundBrush.Color = Color.FromArgb(255, 255, 0, 0);

نصائح أخرى

I have done one work around as below. seems working.

///Kept as arefrence while initial drawing phase.
private DrawingVisual mDrawingVisual = null;

 if (null != mDrawingVisual)
      {
        using (DrawingContext vDrawingContext = mDrawingVisual.RenderOpen())
        {
          DrawingGroup vDrawingGroup = VisualTreeHelper.GetDrawing(mDrawingVisual);
          if (null != vDrawingGroup)
          {
            foreach (Drawing vDrawing in vDrawingGroup.Children)
            {
              GeometryDrawing vGeometryDrawing = vDrawing as GeometryDrawing;
              if (null != vGeometryDrawing)
              {
                vGeometryDrawing.Brush = mBackGroundBrush;
              }
            }
          }

          vDrawingContext.DrawDrawing(vDrawingGroup);
          vDrawingContext.Close();
        }
      }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top