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 saving the data using our internal method (ChangeLinesState), which can be done by using the Reflection method in the WPF EditControl. Refer the code below 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