Click here to Skip to main content
15,895,781 members
Articles / Desktop Programming / WPF

DataObjectBase

Rate me:
Please Sign up or sign in to vote.
4.66/5 (20 votes)
18 May 2010CPOL8 min read 37.9K   572   59  
DataObjectBase -> the new Object class for data objects!
using System.Windows;
using System.Windows.Input;
using CatenaLogic.Data;
using CatenaLogic.Windows;
using Microsoft.Win32;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace CatenaLogic.Examples.UI.Windows
{
    /// <summary>
    /// Interaction logic for BackwardCompatibilityWindow.xaml
    /// </summary>
    public partial class BackwardCompatibilityWindow : DataWindow
    {
        #region Constants
        /// <summary>
        /// Default file dialog filter.
        /// </summary>
        public const string FileDialogFilter = "DataObjectBase files|*.dob|All files|*.*";
        #endregion

        #region Variables
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="BackwardCompatibilityWindow"/> class.
        /// </summary>
        public BackwardCompatibilityWindow()
            : base(DataWindowMode.Close)
        {
            // Initialize component
            InitializeComponent();

            // Add command bindings
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, Save_Executed, Save_CanExecute));
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, Open_Executed, Open_CanExecute));
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets OldPerson.
        /// </summary>
        /// <remarks>
        /// Wrapper for the OldPerson dependency property.
        /// </remarks>
        public Data.OldNamespace.Person OldPerson
        {
            get { return (Data.OldNamespace.Person)GetValue(OldPersonProperty); }
            set { SetValue(OldPersonProperty, value); }
        }

        /// <summary>
        /// DependencyProperty definition as the backing store for OldPerson.
        /// </summary>
        public static readonly DependencyProperty OldPersonProperty =
            DependencyProperty.Register("OldPerson", typeof(Data.OldNamespace.Person), typeof(BackwardCompatibilityWindow), new UIPropertyMetadata(new Data.OldNamespace.Person()));

        /// <summary>
        /// Gets or sets NewPerson.
        /// </summary>
        /// <remarks>
        /// Wrapper for the NewPerson dependency property.
        /// </remarks>
        public Data.NewNamespace.Person NewPerson
        {
            get { return (Data.NewNamespace.Person)GetValue(NewPersonProperty); }
            set { SetValue(NewPersonProperty, value); }
        }

        /// <summary>
        /// DependencyProperty definition as the backing store for NewPerson.
        /// </summary>
        public static readonly DependencyProperty NewPersonProperty =
            DependencyProperty.Register("NewPerson", typeof(Data.NewNamespace.Person), typeof(BackwardCompatibilityWindow), new UIPropertyMetadata(new Data.NewNamespace.Person()));
        #endregion

        #region Command bindings
        /// <summary>
        /// Determines whether the user can execute the Save command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">The event data.</param>
        private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            // Allow by default
            e.CanExecute = true;
        }

        /// <summary>
        /// Handled when the user invokes the Save command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event Arguments.</param>
        private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            // Let the user pick a filename
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = FileDialogFilter;
            if (fileDialog.ShowDialog() ?? false)
            {
                // Save (and notice the great amount of code required to save the old object)
                using (FileStream fileStream = new FileStream(fileDialog.FileName, FileMode.Create, FileAccess.ReadWrite))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(fileStream, OldPerson);
                }
            }
        }

        /// <summary>
        /// Determines whether the user can execute the Open command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">The event data.</param>
        private void Open_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            // Allow by default
            e.CanExecute = true;
        }

        /// <summary>
        /// Handled when the user invokes the Open command.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event Arguments.</param>
        private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            // Let the user pick a filename
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = FileDialogFilter;
            if (fileDialog.ShowDialog() ?? false)
            {
                // Load (and enable redirects)
                NewPerson = Data.NewNamespace.Person.Load(fileDialog.FileName, SerializationMode.Binary, true);
                if (NewPerson == null)
                {
                    // Restore new value
                    NewPerson = new Data.NewNamespace.Person();

                    // Show error
                    MessageBox.Show(Properties.Resources.LoadDataFailed, Properties.Resources.ErrorTitle, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
        #endregion

        #region Methods
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions