SyntaxEditor MVVM approach

1 Answer 128 Views
SyntaxEditor
Florian
Top achievements
Rank 1
Iron
Iron
Florian asked on 30 Nov 2022, 01:18 PM

Hi,

I tried to use RadSyntaxEditor with MVVM approach but I did not succeed.

I followed all tips you gave on the forum but it still doesn't respond to all my needs.

I try to create a custom dependency property and bound my textdocument (located in my view model) in it but the dependency property got the radsyntaxeditor object and not TextDocument or content in TextDocument.

With this solution I was able to initialize the value of the document in RadSyntaxEditor but the callback method was not able to capture modification in the content of the document. So when I do modificaton, I wasn't able to applied them in my TextDocument in VM.

Finally I use events DataContextChange to init and DocumentChangeContent to update mine but it's not MVVM approach...

Do you have some advice for me to succeed in the MVVM approach ?

1 Answer, 1 is accepted

Sort by
0
Accepted
Stenly
Telerik team
answered on 01 Dec 2022, 12:32 PM

Hello Florian,

I have prepared a sample project that contains a RadSyntaxEditor control in an MVVM scenario. The Document property of the control is bound via an attached property and modifications of the file are reflected correctly.

The attached property's implementation is the suggested one from this forum thread:

MVVM example of using the SyntaxEditor in UI for WPF | Telerik Forums

With this being said, could you give the project a try and let me know how it goes?

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Florian
Top achievements
Rank 1
Iron
Iron
commented on 02 Dec 2022, 12:26 PM

Hello Stenly,

I succeed to use your example and it works. 

However, when I put a breakpoint in the set property of the TextDocument in the viewmodel, I never catch it. Same thing if I put a breakpoint in OnTextDocumentChanged, the PropertyChangedCallback method in the dependency property class.

I try to add this to my project and I observe the same result. Modifications were saved in my TextDocument but I never catch the set property. In my project, it's a big problem because I'm supposed to save modifications of the TextDocument in a string parameter. I would like to do it in the set property but as I said I never catch it..

If you have any suggestions, I'd love to hear them.

Stenly
Telerik team
commented on 07 Dec 2022, 07:59 AM

The set method will occur only when the property is assigned to a new value and not when a correction is made to its content.

What I could suggest would be to utilize the DocumentContentChanged event, which will occur every time a change is made to the content of the document. For an MVVM approach, the EventToCommandBehavior could be utilized to execute a command in the view model when this event occurs.

I have modified the sample project from my initial reply to this forum thread to include this suggestion.

Florian
Top achievements
Rank 1
Iron
Iron
commented on 19 Dec 2022, 08:53 AM

Thanks a lot for your help Stenly.

You were really reactive and helpful !

Stenly
Telerik team
commented on 19 Dec 2022, 09:16 AM

Hello Florian,

I am happy to hear that the proposed suggestion from my above comment was of help to you.

Tags
SyntaxEditor
Asked by
Florian
Top achievements
Rank 1
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or