Articles in this section
Category / Section

How to highlight the line color while saving the data as stream in WPF EditControl?

1 min read

You can update the line state after save the data using our internal method (ChangeLinesState), which can be done by using Reflection method in WPF EditControl. Refer the below code for your reference.

C#

_editor.PreviewKeyUp += OnPreviewKeyUp;
_editor.PreviewKeyDown += OnPreviewKeyDown;
 
private void OnPreviewKeyUp(object sender, KeyEventArgs e)
{
  switch (e.Key)
  {
    case Key.RightCtrl:
    case Key.LeftCtrl:
        _isCtrlDown = false;
        break;
    default: break;
  }
}
 
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
  switch (e.Key)
  {
    case Key.RightCtrl:
    case Key.LeftCtrl:
        _isCtrlDown = true;
        break;
    case Key.S:
        if (_isCtrlDown)
        {
          SetFileText(_editor.Text);
          e.Handled = true;
        }
        break;
  }
}
 
private void SetFileText(string text)
{
  using (StreamWriter sw = new StreamWriter(_filePath))
  {
    sw.Write(text);
    MethodInfo methodInfo = this._editor.GetType().GetMethod("ChangeLinesState", BindingFlags.NonPublic | BindingFlags.Instance);
    methodInfo?.Invoke(this._editor, new object[] { LineModificationState.Modified, LineModificationState.Saved});
  }
}

Output:

Highlight the saved data in EditControl

Sample: View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied