BreeceW / WinUIEdit

Code editor control for UWP and WinUI 3 based on Scintilla
Other
101 stars 6 forks source link

MVVM x:Bind to the text of the control not possible (or not obvious) #5

Open TheJoeFin opened 3 months ago

TheJoeFin commented 3 months ago

I would like to use this control in my app, but it seems like there is no way to bind the text to a viewmodel but maybe I just missed something. Looks like a really nice project looking forward to updates!

BreeceW commented 2 months ago

I suggest using the modified event in code behind to update the editor and vice versa. The modified text event would look like this:

private void Editor_Modified(WinUIEditor.Editor sender, WinUIEditor.ModifiedEventArgs args)
{
    if (((ModificationFlags)args.ModificationType).HasFlag(ModificationFlags.InsertText | ModificationFlags.DeleteText))
    {
        ViewModel.Text = sender.GetText(long.MaxValue);
    }
}

And to set from the view model:

private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == nameof(EditorViewModel.Text))
    {
        Editor.Editor.SetText(ViewModel.Text);
    }
}

I’m hesitant to add a Text dependency property since the editor needs to be able to work with files in the size of gigabytes. Getting and setting the text requires a UTF-16 <-> UTF-8 conversion and an extra copy in a potentially massive string. There should be some kind of opt-in dependency property, but I’m not sure what the best way to do that would be.