Quantcast
Viewing all articles
Browse latest Browse all 7205

DXRichEdit for WPF: Implementing MVVM

This example illustrates how to implement a MVVM pattern (see Model View ViewModel) in an application for RTF text editing. Model is represented by the CarInfo class with the Name and Description properties. The CarInfoViewModel class represents a view model. It wraps the model object and exposes its properties to the view which is represented by the EditView class. The view model implements the INotifyPropertyChanged Interface for the properties it exposes so that the view can easily data bind to them. The actual data binding is defined in the EditView.xaml file via a XAML binding of the RichEditControl.Content Property to the Description property of a view model, which in turn, is defined in the UserControl's data context:

[XML]
<UserControlx:Class="RichEditMVVMScenarioWpf.View.EditView"...><UserControl.DataContext><vm:CarInfoViewModel/></UserControl.DataContext><UserControl.Resources><dxre:RtfToContentConverterx:Key="rtfToContentConverter"/></UserControl.Resources> ... <dxre:RichEditControlName="reDescription"Content="{Binding Description, Converter={StaticResource rtfToContentConverter}, Mode=TwoWay}"/></UserControl>

Note that the RtfToContentConverter is used for binding. See the DXRichEdit for WPF: How to use RichEdit converters to bind a RichEditControl to a particular entity and vice versa example to learn more on this concept.

The view model also contains Load and Save commands that are used to persist model property values in the database. This logic is implemented via regular ADO.NET methods.

Prior to running this example, it is required to register a "CarsDB" sample database on your local SQL Server instance. You can download the corresponding SQL scripts from the How to use a RichEditControl in bound mode example.

The picture below illustrates the sample in action.

Image may be NSFW.
Clik here to view.

See also:
WPF Apps With The Model-View-ViewModel Design Pattern
WPF/MVVM Quick Start Tutorial


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>