Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have class which displays line no using OnRender Method. now i need to call that method from other class on Text Changed event. how to trigger OnRender method

C#
public class EditorControl : Control
{
    void EditorTextBoxControl_TextChanged(object sender, TextChangedEventArgs e)
            {
                if(_bIsNewScriptLoaded)
                RaiseEditorTextChangedEvent();
        //need to call here
     }
}
 public class LineMargin : FrameworkElement
{
 protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
 {
	Rect rect = EditorText.GetRectFromCharacterIndex(charIndex);
         	   //this is the height of each line in textEditor.
         	   if (rect.IsEmpty == true)
          	      lineHeight = 0;
          	  else
          	      lineHeight = rect.Height;

         	   for (int i = 0; lineNumber <= _Controller.GetLineCount(); i++, lineNumber++)
           	 {
            	  	  //var foreground = (Brush)GetValue(Control.ForegroundProperty);
                
              	 	 FormattedText text = new FormattedText(
                                                        lineNumber.ToString(CultureInfo.CurrentCulture),
                                                        CultureInfo.GetCultureInfo("en-us"),
                                                        FlowDirection.LeftToRight,
                                                        new Typeface("Verdana"),
                                                        emSize,
                                                        Brushes.SteelBlue);

           	    	 drawingContext.DrawText(text, new Point(renderSize.Width - text.Width,
                                                        (lineHeight * i)  + 2));//2 is a buffer as the number should be displayed little below the rectangle height.
         	   }	
}
}
Posted
Updated 28-Sep-11 13:59pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Sep-11 18:18pm    
What do you want to achieve?
--SA

Bad approach. You don't need to re-render anything, so, no need to call OnRender. If you cause re-rendering, this method will be called anyway. It's the reason why this event is virtual protected, not public. Maybe your mistake is overriding and calling your code from this method.

Think about it: if you wanted to call your code every time rendering happens, you would not have this problem. Consequently, you need to call this code even in some cases when rendering does not happen, so the question is: why putting this code in OnRender? It looks like you simply could not find better place. Now, no more comment on this part, as you did not explain the ultimate goal of this activity — think about it by yourself.

Now, if you still want to follow this approach, a formal and safe resolution is this: make instance another method (let's say, F) and put there all the code you execute in the method into F. Now, call F from OnRender — this will give your the functionality you have right now. Additionally, add the access modifier internal or even public (access from other assemblies) to F — this will make it callable from other classes. That's it.

However, I don't think this is useful by the reasons I explained above. You seemingly trying to abuse WPF. Again, I have nothing useful to add to it as you did not share the goal of this activity.

—SA
 
Share this answer
 
by calling obj.InvalidateVisual() its a defined method in WPF Framework we can use it for re rendering element to the layout
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900