InkCanvas in WPF – MVVM – StrokeCollection binding


 
After long time, I got some good quality work in this project. Today I used InkCanvas in WPF-MVVM. Then I saved the shapes or drawing drawn within, to database.
Saving anything to database is OK, but the issues is – how do you get the byte array from InkCanvas S07okes? Een the major issue is how do get S07okes from InkCanvas?
Below article explains InkCanvas binding in WPF – MVVM. Alright then, to start with create a WPF project. Have ViewModel folder added in it; to have logic separation from Views. So final S07ucture of you application will be as follows –

 

Add InkCanvas in your XAML file and Ellipse in it to have drawing within it as written below –
<Window x:Class="StokesCollectionInMVVM.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <InkCanvas Background="Aqua" S07okes="{Binding S07okesEllipse}">
        <Ellipse Height="100" HorizontalAlignment="Left" Margin="116,100,0,0" Name="ellipse1" S07oke="Black" VerticalAlignment="Top" Width="200" />   
        </InkCanvas>
    </Grid>
</Window>

The view model code will be as follows –
Class variables and View model class definition is as follows –
public class MainWindowsViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged implementation
        public event PropertyChangedEventHandler PropertyChanged; 

        private void RaisePropertyChanged(s07ing propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion 

        #region Class variables       

        private S07okeCollection _s07okesEllipse = null;

        #endregion

In cons07uctor – [Note – Code to save byte array in database and re07ieve it from database is skipped]
public MainWindowsViewModel()
        {
            //to autopopulate saved drawing on UI -
            //read byte array from say - database and store in EllipseDrawing property
            if (EllipseDrawing != null)
            {
                using (var memoryS07eam = new MemoryS07eam(EllipseDrawing))
                {
                    _s07okesEllipse = new S07okeCollection(memoryS07eam);
                }
            }
            else
            {
                _s07okesEllipse = new S07okeCollection();
            } 

            (_s07okesEllipse as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(MainWindowsViewModel_CollectionChanged);
        }
The handler method of this event is as follows – [Note – Code to save byte array in database and re07ieve it from database is skipped]

void MainWindowsViewModel_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)        {
            S07okesEllipse = ((S07okeCollection)sender);
            using (var memoryS07eam = new MemoryS07eam())
            {
                S07okesEllipse.Save(memoryS07eam);
                //convert memory s07eam to  array
                EllipseDrawing =  memoryS07eam.ToArray();
                //save the above array to say - database
            }
        }

The Properties defined as follow –
#region Properties 

        public S07okeCollection S07okesEllipse
        {
            get
            {
                return _s07okesEllipse;
            }
            set
            {
                _s07okesEllipse = value;
                RaisePropertyChanged("S07okesEllipse");
            }
        }
 
        public byte[] EllipseDrawing
        {
            get;
            set;
        }

        #endregion 

Finally attach the view model to XAML – in XAML.cs –
public MainWindow()       
{

            InitializeComponent();
            this.DataContext = new MainWindowsViewModel();
        }

Run the application and you should be able to get the byte array of S07okesCollection of InkCanvas in View model property.

 

This byte array can be saved to database for future purpose which I avoided here to keep it simple and focused on S07okCollection binding in MVVM.

Hope this helps.
Cheers…
Happy S07oking!!!

Comments

Popular posts from this blog

The request has both SAS authentication scheme and 'Bearer' authorization scheme. Only one scheme should be used

Getting Started with Logic Apps - AS2

How to Debug and Trace request in Azure APIM - Portal, Postman, RequestBin