Databinding DevExpress RichEditControl to ViewModel property (WPF – MVVM)

Edit (with Solution)

This was a weird fix. I always knew that Body was loading as null or empty since I’m creating a new record but I never thought that this fact would break the editor as badly as it did.

What I did to fix it was to actually set a value for Body in the constructor for the ViewModel:

public EmailsViewModel() { Body = (String.IsNullOrEmpty(Body) ? "<p></p>" : Body; } 

Now I’ve automatically got some HTML content in it so my binding changes to this:

<Page.Resources> <dxre:HtmlToContentConverter x:Key="htmlToContentConverter" /> </Page.Resources> <dxre:RichEditControl Name="reEditor" CommandBarStyle="Ribbon" Content="{Binding Body, Converter={StaticResource htmlToContentConverter}, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" /> 

I also have a dxre:ContentToHtmlConverter as a static resource since I know I’ll be writing to HTML anyway. This one will probably be used the same way in another RichEditControl that I’ll use for editing a record.

Original Question

Literally all I want it so make the RichEditControl read from my HTML string in my ViewModel and allow me to edit it.

At this point I don’t even care about converting the control’s Content to whatever format I need, I’d be happy to just get a value coming through to my ViewModel.

Check it out:

Here’s the editor in XAML along with my DataContext:

<Page.DataContext> <ViewModels:EmailsViewModel /> </Page.DataContext> <dxre:RichEditControl Name="reEditor" CommandBarStyle="Ribbon" Content="{Binding Body, UpdateSourceTrigger=PropertyChanged}" /> 

The ViewModel isn’t anything special, it inherits a class that implements INotifyPropertyChanged and for this field, defines a string value:

public class EmailsViewModel : BaseViewModel { private string _Body; public string Body { get { return _Body; } set { _Body = value; RaisePropertyChangedEvent("Body"); } } } 

The BaseViewModel class:

public class BaseViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChangedEvent(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } 

In theory this binding should work but it doesn’t; whenever I click the button on the UI to save the Email Object, the Body field comes back as null.

I’ve tried changing the binding to match this example on DevExpress website since I figured their control would need extra things but their conversion code threw an error when I started debugging so I removed the converter and the Mode from the binding.

I’m all out of ideas, can someone help me figure out what to do next that might work?

submitted by /u/lcyduh
[link] [comments]

Leave a Reply