WPF Ruler Performance issue
===

I have Ruler, which looks like this.

Image : http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a7a6bbec-e870-4f1a-8c79-feff29a070c0

Complete code is written in OnRender to generate all margins and red chip. But When I move mouse, every time it calls OnRender function and re generate whole Ruler.

Issue is: If my window size is too big than it take 4 to 5 sec to draw chip and create whole ruler. It causes performance issue. Every mouse move, it calls OnRender function to do that.

I know it's doing because of I have created dependency property for Red Chip like this:

    public static readonly DependencyProperty ChipProperty =
             DependencyProperty.Register("Chip", typeof(double), typeof(Ruler),
                  new FrameworkPropertyMetadata((double)-1000, FrameworkPropertyMetadataOptions.AffectsRender));

I tried to do something like this to avoid call of OnRender function every time I move mouse.

     public static readonly DependencyProperty ChipProperty =
             DependencyProperty.Register("Chip", typeof(double), typeof(Ruler),
                  new FrameworkPropertyMetadata((double)-1000, OnValueChanged));


     private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Ruler wr = obj as Ruler;
            double chip = wr.Unit == Unit.Cm ? DipHelper.CmToDip(wr.Chip) : DipHelper.InchToDip(wr.Chip);
            DrawingVisual dLine = new DrawingVisual();
            using (DrawingContext dc = dLine.RenderOpen())
            {
                dc.DrawLine(wr.RedPen, new Point(chip, 0), new Point(chip, wr.Height));

             // It's not drawing line as per position. .

            }
        }

Please help. Thanks.


  [1]: http://i.stack.imgur.com/85MkQ.png