Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / WPF

Building WPF Applications with Self-Tracking Entity Generator and Visual Studio 2012 - Project Setup

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
17 Mar 2013CPOL8 min read 68.5K   3.5K   44  
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator and Visual Studio 2012.
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using GalaSoft.MvvmLight;
using SchoolSample.Common;

namespace SchoolSample
{
    /// <summary>
    /// Interaction logic for CoursePage.xaml
    /// </summary>
    public partial class CoursePage
    {
        #region "Constructor"

        public CoursePage()
        {
            InitializeComponent();

            // register for CultureChangeMessage
            AppMessages.CultureChangeMessage.Register(this, OnCultureChangeMessage);
            // register for BeginEditMessage
            AppMessages.BeginEditMessage.Register(this, OnBeginEditMessage);
            // register for EndEditMessage
            AppMessages.EndEditMessage.Register(this, OnEndEditMessage);
            // register for CancelEditMessage
            AppMessages.CancelEditMessage.Register(this, OnCancelEditMessage);

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // Use MEF To load the View Model
                DataContext = App.Container.GetExportedValue<ViewModelBase>(
                    ViewModelTypes.CoursePageViewModel);
            }

            // set up OneWayToSource binding from courseValidationSummary
            // to CurrentCourseHasErrors
            var courseBinding = new Binding
                                    {
                                        Path = new PropertyPath("CurrentCourseHasErrors"),
                                        Mode = BindingMode.OneWayToSource
                                    };
            courseValidationSummary.SetBinding(ValidationSummary.HasErrorsProperty, courseBinding);
            // set up OneWayToSource binding from enrollmentValidationSummary
            // to CurrentEnrollmentHasErrors
            var enrollmentBinding = new Binding
                                        {
                                            Path = new PropertyPath("CurrentEnrollmentHasErrors"),
                                            Mode = BindingMode.OneWayToSource
                                        };
            enrollmentValidationSummary.SetBinding(ValidationSummary.HasErrorsProperty, enrollmentBinding);
        }

        #endregion "Constructor"

        #region "CultureChangeMessage"

        private void OnCultureChangeMessage(string culture)
        {
            switch (culture)
            {
                case "en":
                    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
                    break;
                case "zh-Hans":
                    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-Hans");
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-Hans");
                    break;
                case "zh-Hant":
                    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-Hant");
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-Hant");
                    break;
            }
            titleLabel.Refresh();
            titleDescriptionViewer.Refresh();
            instructorLabel.Refresh();
            instructorDescriptionViewer.Refresh();
            studentLabel.Refresh();
            studentDescriptionViewer.Refresh();
            paidLabel.Refresh();
            paidDescriptionViewer.Refresh();
            startDateLabel.Refresh();
            startDateDescriptionViewer.Refresh();
            endDateLabel.Refresh();
            endDateDescriptionViewer.Refresh();
            classSizeLabel.Refresh();
            classSizeDescriptionViewer.Refresh();
        }

        #endregion "CultureChangeMessage"

        #region "BeginEditMessage"

        private void OnBeginEditMessage(string screenName)
        {
            switch (screenName)
            {
                case "Course":
                    titleLabel.Foreground = new SolidColorBrush(Colors.Black);
                    titleTextBox.IsEnabled = true;
                    titleDescriptionViewer.Visibility = Visibility.Visible;
                    instructorLabel.Foreground = new SolidColorBrush(Colors.Black);
                    instructorComboBox.IsHitTestVisible = true;
                    instructorComboBox.IsTabStop = true;
                    instructorComboBox.Foreground = new SolidColorBrush(Colors.Black);
                    instructorDescriptionViewer.Visibility = Visibility.Visible;
                    startDateLabel.Foreground = new SolidColorBrush(Colors.Black);
                    startDateDatePicker.IsEnabled = true;
                    startDateDescriptionViewer.Visibility = Visibility.Visible;
                    endDateLabel.Foreground = new SolidColorBrush(Colors.Black);
                    endDateDatePicker.IsEnabled = true;
                    endDateDescriptionViewer.Visibility = Visibility.Visible;
                    classSizeLabel.Foreground = new SolidColorBrush(Colors.Black);
                    classSizeMaskedTextBox.IsEnabled = true;
                    classSizeDescriptionViewer.Visibility = Visibility.Visible;
                    var courseBinding = new Binding();
                    courseBinding.Source = (App.Current.Resources["ApplicationResources"]);
                    courseBinding.Path = new PropertyPath("Strings.ButtonCommit");
                    editCommitCourseButton.SetBinding(Button.ContentProperty, courseBinding);
                    break;
                case "Enrollment":
                    studentLabel.Foreground = new SolidColorBrush(Colors.Black);
                    studentComboBox.Foreground = new SolidColorBrush(Colors.Black);
                    studentDescriptionViewer.Visibility = Visibility.Visible;
                    paidLabel.Foreground = new SolidColorBrush(Colors.Black);
                    paidCheckBox.IsEnabled = true;
                    paidDescriptionViewer.Visibility = Visibility.Visible;
                    var enrollmentBinding = new Binding();
                    enrollmentBinding.Source = (App.Current.Resources["ApplicationResources"]);
                    enrollmentBinding.Path = new PropertyPath("Strings.ButtonCommit");
                    editCommitEnrollmentButton.SetBinding(Button.ContentProperty, enrollmentBinding);
                    break;
            }
        }

        #endregion "BeginEditMessage"

        #region "EndEditMessage"

        private void OnEndEditMessage(string screenName)
        {
            switch (screenName)
            {
                case "Course":
                    titleLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    titleTextBox.IsEnabled = false;
                    titleDescriptionViewer.Visibility = Visibility.Collapsed;
                    instructorLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    instructorComboBox.IsHitTestVisible = false;
                    instructorComboBox.IsTabStop = false;
                    instructorComboBox.Foreground = new SolidColorBrush(Colors.Gray);
                    instructorDescriptionViewer.Visibility = Visibility.Collapsed;
                    startDateLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    startDateDatePicker.IsEnabled = false;
                    startDateDescriptionViewer.Visibility = Visibility.Collapsed;
                    endDateLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    endDateDatePicker.IsEnabled = false;
                    endDateDescriptionViewer.Visibility = Visibility.Collapsed;
                    classSizeLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    classSizeMaskedTextBox.IsEnabled = false;
                    classSizeDescriptionViewer.Visibility = Visibility.Collapsed;
                    var courseBinding = new Binding();
                    courseBinding.Source = (App.Current.Resources["ApplicationResources"]);
                    courseBinding.Path = new PropertyPath("Strings.ButtonEdit");
                    editCommitCourseButton.SetBinding(Button.ContentProperty, courseBinding);
                    break;
                case "Enrollment":
                    studentLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    studentComboBox.Foreground = new SolidColorBrush(Colors.Gray);
                    studentDescriptionViewer.Visibility = Visibility.Collapsed;
                    paidLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    paidCheckBox.IsEnabled = false;
                    paidDescriptionViewer.Visibility = Visibility.Collapsed;
                    var enrollmentBinding = new Binding();
                    enrollmentBinding.Source = (App.Current.Resources["ApplicationResources"]);
                    enrollmentBinding.Path = new PropertyPath("Strings.ButtonEdit");
                    editCommitEnrollmentButton.SetBinding(Button.ContentProperty, enrollmentBinding);
                    break;
            }
        }

        #endregion "EndEditMessage"

        #region "CancelEditMessage"

        private void OnCancelEditMessage(string screenName)
        {
            switch (screenName)
            {
                case "Course":
                    titleLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    titleTextBox.IsEnabled = false;
                    titleDescriptionViewer.Visibility = Visibility.Collapsed;
                    instructorLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    instructorComboBox.IsHitTestVisible = false;
                    instructorComboBox.IsTabStop = false;
                    instructorComboBox.Foreground = new SolidColorBrush(Colors.Gray);
                    instructorDescriptionViewer.Visibility = Visibility.Collapsed;
                    startDateLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    startDateDatePicker.IsEnabled = false;
                    startDateDescriptionViewer.Visibility = Visibility.Collapsed;
                    endDateLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    endDateDatePicker.IsEnabled = false;
                    endDateDescriptionViewer.Visibility = Visibility.Collapsed;
                    classSizeLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    classSizeMaskedTextBox.IsEnabled = false;
                    classSizeDescriptionViewer.Visibility = Visibility.Collapsed;
                    var courseBinding = new Binding();
                    courseBinding.Source = (App.Current.Resources["ApplicationResources"]);
                    courseBinding.Path = new PropertyPath("Strings.ButtonEdit");
                    editCommitCourseButton.SetBinding(Button.ContentProperty, courseBinding);
                    break;
                case "Enrollment":
                    studentLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    studentComboBox.Foreground = new SolidColorBrush(Colors.Gray);
                    studentDescriptionViewer.Visibility = Visibility.Collapsed;
                    paidLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    paidCheckBox.IsEnabled = false;
                    paidDescriptionViewer.Visibility = Visibility.Collapsed;
                    var enrollmentBinding = new Binding();
                    enrollmentBinding.Source = (App.Current.Resources["ApplicationResources"]);
                    enrollmentBinding.Path = new PropertyPath("Strings.ButtonEdit");
                    editCommitEnrollmentButton.SetBinding(Button.ContentProperty, enrollmentBinding);
                    break;
            }
        }

        #endregion "CancelEditMessage"
    }
}

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 (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions