Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TheJoeFin opened this issue Jan 24, 2024 · 1 comment
Open

Comments

@TheJoeFin
Copy link

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
Copy link
Owner

BreeceW commented Feb 13, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants