wpfwpf-graphics

How to change color of a DrawingVisual on MouseHit


The following code is an adaptation of the code example on page 502 of Adam Nathan, WPF 4.5 unleashed.

Following the advice there I created a class VisualHostClass that "hosts" a list of DrawingVisuals, thereby making them displayable. This worked so far.

Now I wanted to react to a left mouse click by changing the color of the displayed visual. The code for this is in the method HitTestCallback.

With the included Trace.WriteLine statements I can see that the relevant line

drw.Brush = Brushes.Red;

is reached and the property Brush has a new value afterwards. But: The display does not change, the clicked-on DrawingVisual is still Yellow afterwards (that was the brush color used on creation).

What can/should I do here? (I am absolute beginner with WPF).

Thanks in advance for all answers!

    public partial class VisualHostClass : FrameworkElement
    {
        public VisualHostClass()
        {
 
        }

        public List<DrawingVisual> myVisuals = new List<DrawingVisual>();

        public void AddVisual(DrawingVisual dvs)
        {
            myVisuals.Add(dvs);
            AddVisualChild(dvs);
            AddLogicalChild(dvs);

        }

        protected override int VisualChildrenCount
        {
            get { return myVisuals.Count; }
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= myVisuals.Count)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            return myVisuals[index];
        }

        public void removeAllChilds()
        {
            foreach (DrawingVisual dvs in myVisuals)
            {
                RemoveVisualChild(dvs);
                RemoveLogicalChild(dvs);
            }
            myVisuals.Clear();
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            Point location = e.GetPosition(this);

            VisualTreeHelper.HitTest(this, null,
                new HitTestResultCallback(HitTestCallback),
                new PointHitTestParameters(location));

        }

        public HitTestResultBehavior HitTestCallback(HitTestResult result)
        {
            if (result.VisualHit.GetType() == typeof(DrawingVisual))
            {
                DrawingVisual dv = result.VisualHit as DrawingVisual;

                Trace.WriteLine("HitTestCallback: dv = " + dv.ToString());

                foreach(GeometryDrawing drw in dv.Drawing.Children)
                {
                    Trace.WriteLine("HitTestCallback: drw = " + drw.ToString());
                    
                    Trace.WriteLine("HitTestCallback: drw.Geometry = " + drw.Geometry.ToString());

                    Trace.WriteLine("HitTestCallback: drw.Brush = " + drw.Brush.ToString());

                    drw.Brush = Brushes.Red;

                    Trace.WriteLine("HitTestCallback: drw.Brush = " + drw.Brush.ToString());

                    Trace.WriteLine("HitTestCallback: drw.IsFrozen = " + drw.IsFrozen.ToString());

                }
 
            }

            return HitTestResultBehavior.Continue;
        }
    }
}

EDIT: After some experimenting the following worked (but it is not satifying for me, as I can only change the Color of a SolidColorBrush, but not the Brush itself):

   public void ChangeBrush(DrawingVisual dv)
    {
        Trace.WriteLine("HitTestCallback: dv = " + dv.ToString());

        foreach (Drawing drw1 in dv.Drawing.Children)
        {
            if (drw1 is GeometryDrawing)
            {
                GeometryDrawing drw = drw1 as GeometryDrawing;

                Trace.WriteLine("HitTestCallback: drw = " + drw.ToString());

                Trace.WriteLine("HitTestCallback: drw.Geometry = " + drw.Geometry.ToString());

                Trace.WriteLine("HitTestCallback: drw.Brush = " + drw.Brush.ToString());

                Trace.WriteLine("HitTestCallback: drw.IsFrozen = " + drw.IsFrozen.ToString());
                Trace.WriteLine("HitTestCallback: drw.Brush.IsFrozen = " + drw.Brush.IsFrozen.ToString());

                // does not work
                //SolidColorBrush newBrush = new SolidColorBrush(Colors.Red);

                //newBrush.Freeze();

                //drw.Brush = newBrush;

                // works with drw.Brush already containing a SolidColorBrush
                (drw.Brush as SolidColorBrush).Color =
                    (drw.Brush as SolidColorBrush).Color == Colors.LightGoldenrodYellow ?
                    Colors.Red : Colors.LightGoldenrodYellow;

                Trace.WriteLine("HitTestCallback: drw.Brush = " + drw.Brush.ToString());

                Trace.WriteLine("HitTestCallback: drw.IsFrozen = " + drw.IsFrozen.ToString());
                Trace.WriteLine("HitTestCallback: drw.Brush.IsFrozen = " + drw.Brush.IsFrozen.ToString());

            }
        }


    }

Just for the record I cite the code where the DrawingVisual is constructed and put in VisualHostClass

        PathGeometry pgnGeom = RenderPolygonWithHoles2Geometry(pgn);

        DrawingVisual dv = new DrawingVisual();

        using (DrawingContext dc = dv.RenderOpen())
        {
            SolidColorBrush brush1 = new SolidColorBrush(Colors.LightGoldenrodYellow);
            dc.DrawGeometry(brush1, null, pgnGeom);
        }

        myVisualHost.AddVisual(dv);

Solution

  • It appears that the Drawing created by dc.DrawGeometry(...) is frozen, i.e. not modifiable.

    Passing my own Drawing has worked for me:

    using (var dc = dv.RenderOpen())
    {
        dc.DrawDrawing(new GeometryDrawing(Brushes.LightGoldenrodYellow, null, pgnGeom));
    }