Forums

HOW-TO: MVVM and Data Binding
Last Post 29 Feb 2016 04:13 PM by Stanislav Pitsul. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Filip DušekUser is Offline
Advanced Member
Advanced Member
Posts:674


--
27 Sep 2014 11:59 AM
    All important classes for MVVM are in namespace EmptyKeys.UserInterface.Mvvm. You can use BindableBase class as base class for your Models and ViewModelBase for your View Models. There is Service Manager for all MVVM services you need. By default there is MessageBoxService implemented.

    Example how to use it:

                var messageBoxService = base.GetService<IMessageBoxService>();
                if (messageBoxService != null)
                {
                    RelayCommand result = new RelayCommand(new Action<object>(OnResetSettingsMessageBoxResult));
                    var localizationService = base.GetService<ILocalizationService>();
                    if (localizationService != null)
                    {
                        string question = localizationService.GetLocalizedString("ResetSettingsQuestion", CultureInfo.CurrentCulture);
                        string caption = localizationService.GetLocalizedString("ResetSettingsCaption", CultureInfo.CurrentCulture);
                        messageBoxService.Show(question, caption, MessageBoxButton.YesNo, result);
                    }
                }
    


    Example of FileStorageService:

    
        public class FileStorageService : IFileStorage
        {
            /// <summary>
            /// Loads the file stream asynchronous.
            /// </summary>
            /// <param name="filename">The filename.</param>
            /// <returns></returns>
            public Task<Stream> LoadFileStreamAsync(string filename)
            {
                using (var sr = new FileStream(filename, FileMode.Open))
                {
                    Stream stream = new MemoryStream();
                    sr.CopyTo(stream);
                    return Task.FromResult(stream);
                }
            }
    
            /// <summary>
            /// Loads the file text asynchronous.
            /// </summary>
            /// <param name="filename">The filename.</param>
            /// <returns></returns>
            public Task<string> LoadFileTextAsync(string filename)
            {
                string content = string.Empty;
                if (File.Exists(filename))
                {
                    using (var sr = File.OpenText(filename))
                    {
                        content = sr.ReadToEnd();
                    }
                }
    
                return Task.FromResult(content);
            }
    
            /// <summary>
            /// Saves the file asynchronous.
            /// </summary>
            /// <param name="filename">The filename.</param>
            /// <param name="content">The content.</param>
            /// <returns></returns>
            public Task SaveFileAsync(string filename, string content)
            {
                using (var sw = File.CreateText(filename))
                {
                    sw.Write(content);
                }
    
                return Task.FromResult(true);
            }
    
            /// <summary>
            /// Saves the file asynchronous.
            /// </summary>
            /// <param name="filename">The filename.</param>
            /// <param name="content">The content.</param>
            /// <returns></returns>
            public Task SaveFileAsync(string filename, Stream content)
            {
                using (var fs = new FileStream(filename, FileMode.Create))
                {
                    content.CopyTo(fs);
                }
    
                return Task.FromResult(true);
            }
        }
    
    


    Data Binding

    Binding (and TemplateBinding in templates) to Dependency Property or class with INotifyPropertyChanged is supported. For example:

    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
    
    <TextBlock Name="buttonResult" Text="{Binding ButtonResult}" HorizontalAlignment="Center" />
    
    Stanislav PitsulUser is Offline
    New Member
    New Member
    Posts:10


    --
    28 Feb 2016 12:24 PM
    Could you explain about method:
    BindableBase.AcceptChanges(); 

    because I don't understand what it's doing?
    I supposed that it notified about all properties are changed in a ViewModel but it's wrong.
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    28 Feb 2016 12:35 PM
    It's very simple IsDirty/AcceptChanges feature. Method does not do any notification, it's just setting IsDirty property to false. This feature is useful in case when you want to know that object was somehow changed. Then you save it for example and call AcceptChanges.
    Stanislav PitsulUser is Offline
    New Member
    New Member
    Posts:10


    --
    29 Feb 2016 04:13 PM
    Thanks for clarification.
    You are not authorized to post a reply.