Commit 66cd7f7a authored by Greg Pettyjohn's avatar Greg Pettyjohn
Browse files

Code cleanup + style cop. Checking in for demo

parent b8e1484d
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
using System.Windows;
//----------------------------------------------------------------------------------------------
// <copyright file="App.xaml.cs" company="Microsoft Corporation">
//     Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
//----------------------------------------------------------------------------------------------
using System.Windows;

namespace DeviceLab
{
+36 −0
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//----------------------------------------------------------------------------------------------
// <copyright file="AutoScrollTextBox.cs" company="Microsoft Corporation">
//     Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
//----------------------------------------------------------------------------------------------
using System.Windows;
using System.Windows.Controls;

namespace DeviceLab
{
    /// <summary>
    /// A TextBox derrivative that automatically scrolls to end whenever new text is added.
    /// This is used for presenting scrolling output spew.
    /// </summary>
    public class AutoScrollTextBox : TextBox
    {
        /// <summary>
        /// Initializes static members of the <see cref="AutoScrollTextBox" /> class.
        /// </summary>
        static AutoScrollTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AutoScrollTextBox), new FrameworkPropertyMetadata(typeof(AutoScrollTextBox)));
        }

        /// <summary>
        /// Override OnTextChanged to automatically scroll to the end
        /// </summary>
        /// <param name="e">The arguments associated with this event</param>
        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            base.OnTextChanged(e);
            CaretIndex = Text.Length;
            ScrollToEnd();
            this.CaretIndex = Text.Length;
            this.ScrollToEnd();
        }
    }
}
+167 −0
Original line number Diff line number Diff line
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
//----------------------------------------------------------------------------------------------
// <copyright file="NumberEntryBox.xaml.cs" company="Microsoft Corporation">
//     Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
//----------------------------------------------------------------------------------------------
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Prism.Commands;

namespace DeviceLab
{
@@ -23,21 +16,44 @@ namespace DeviceLab
    /// </summary>
    public partial class NumberEntryBox : UserControl, INotifyPropertyChanged
    {
        //-------------------------------------------------------------------
        // Constructors
        //-------------------------------------------------------------------
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="NumberEntryBox" /> class.
        /// </summary>
        public NumberEntryBox()
        {
            InitializeComponent();
            this.InitializeComponent();
        }
        #endregion // Constructors

        //-------------------------------------------------------------------
        // Dependency Properties
        //-------------------------------------------------------------------
        #region DependencyProperties
        #region Value Dependency Property
        /// <summary>
        /// Gets or sets the instance property backing the Value Dependency Property
        /// </summary>
        public int Value
        {
            get { return (int)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
            set { this.SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        /// <summary>
        /// Value Dependency Property static association
        /// </summary>
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(int), typeof(NumberEntryBox), new PropertyMetadata(0, OnValueChanged, CoerceValue));

        /// <summary>
        /// Forward the dependency property changes through the INotifyPropertyChanged interface
        /// </summary>
        /// <param name="d">Dependency opbject that bears the property's value</param>
        /// <param name="e">The arguments associated with this event</param>
        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NumberEntryBox thisNEB = d as NumberEntryBox;
@@ -47,36 +63,59 @@ namespace DeviceLab
            }
        }

        /// <summary>
        /// Coerce the value to keep it inside the expected range. The value should not be less than 1
        /// </summary>
        /// <param name="d">Dependency opbject that bears the property's value</param>
        /// <param name="baseValue">The value the property would have taken before being coerced</param>
        /// <returns>The coerced value</returns>
        private static object CoerceValue(DependencyObject d, object baseValue)
        {
            int val = (int)baseValue > 0 ? (int)baseValue : 1;
            return val;
        }
        #endregion // Value Dependency Property
        #endregion // Dependency Properties

        //-------------------------------------------------------------------
        // Commands
        //-------------------------------------------------------------------
        #region Commands
        #region DecrementCommand
        /// <summary>
        /// The DecrementCommand decrements the number in the Value Depencency Property
        /// </summary>
        private DelegateCommand decrementCommand;
        
        /// <summary>
        /// Gets the DecrementCommand
        /// </summary>
        public ICommand DecrementCommand
        {
            get
            {
                if (this.decrementCommand == null)
                {
                    this.decrementCommand = new DelegateCommand(ExecuteDecrement, CanExecuteDecrement);
                    this.decrementCommand.ObservesProperty(() => Value);
                    this.decrementCommand = new DelegateCommand(this.ExecuteDecrement, this.CanExecuteDecrement);
                    this.decrementCommand.ObservesProperty(() => this.Value);
                }

                return this.decrementCommand;
            }
        }

        /// <summary>
        /// Predicate for the DecrementCommand
        /// </summary>
        /// <returns>True if the command can be executed</returns>
        private bool CanExecuteDecrement()
        {
            return this.Value > 1;
        }

        /// <summary>
        /// Performs the operation of the DecrementCommand
        /// </summary>
        private void ExecuteDecrement()
        {
            this.Value = this.Value - 1;
@@ -84,19 +123,30 @@ namespace DeviceLab
        #endregion // DecrementCommand

        #region IncrementCommand
        /// <summary>
        /// The DecrementCommand decrements the number in the Value Depencency Property
        /// </summary>
        private DelegateCommand incrementCommand;

        /// <summary>
        /// Gets the IncrementCommand
        /// </summary>
        public ICommand IncrementCommand
        {
            get
            {
                if (this.incrementCommand == null)
                {
                    this.incrementCommand = new DelegateCommand(ExecuteIncrement);
                    this.incrementCommand = new DelegateCommand(this.ExecuteIncrement);
                }

                return this.incrementCommand;
            }
        }

        /// <summary>
        /// Performs the operation of the Increment command
        /// </summary>
        private void ExecuteIncrement()
        {
            this.Value = this.Value + 1;
@@ -104,6 +154,14 @@ namespace DeviceLab
        #endregion // IncrementCommand
        #endregion // Commands

        //-------------------------------------------------------------------
        // INotifyPropertyChanged implementation
        //-------------------------------------------------------------------
        #region INotifyPropertyChanged implementation
        /// <summary>
        /// PropertyChanged event for the INotifyPropertyChanged implementation
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion // INotifyPropertyChanged implementation
    }
}
+38 −14
Original line number Diff line number Diff line
using System;
//----------------------------------------------------------------------------------------------
// <copyright file="SelectionListBox.cs" company="Microsoft Corporation">
//     Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
//----------------------------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

@@ -11,32 +11,44 @@ namespace DeviceLab
{
    /// <summary>
    /// Custom ListBox that exposes a SelectionList Dependency property to enable two-way binding.
    /// Internally, SelectionList is kept in sink with the SelectedItems property of the base
    /// class
    /// Internally, SelectionList is kept in sink with the SelectedItems property of the base class
    /// </summary>
    public class SelectionListBox : ListBox
    {
        //-------------------------------------------------------------------
        // Constructors
        //-------------------------------------------------------------------
        #region Constructors
        /// <summary>
        /// Initializes static members of the <see cref="SelectionListBox" /> class.
        /// </summary>
        static SelectionListBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SelectionListBox), new FrameworkPropertyMetadata(typeof(SelectionListBox)));
        }
        #endregion // Cosntructors

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
            SetValue(SelectionListProperty, this.SelectedItems);
        }
        //-------------------------------------------------------------------
        // Depenency Properties
        //-------------------------------------------------------------------
        #region Dependency Properties
        #region SelectionList Dependency Property

        /// <summary>
        /// Gets or sets the SelectionList dependency property for the instance of the class
        /// </summary>
        public IList SelectionList
        {
            get { return (IList)GetValue(SelectionListProperty); }
            set { SetValue(SelectionListProperty, value); }
            set { this.SetValue(SelectionListProperty, value); }
        }

        /// <summary>
        /// SelectionList Dependency Property static association
        /// </summary>
        public static readonly DependencyProperty SelectionListProperty =
            DependencyProperty.Register("SelectionList", typeof(IList), typeof(SelectionListBox), new PropertyMetadata(null, null, CoerceSelectionList));
 
 
        /// <summary>
        /// Coerce the value of SelectionList so that it is identical to (i.e. the same object as) the
        /// value of SelectedItems
@@ -73,5 +85,17 @@ namespace DeviceLab

            return selectedItems;
        }

        /// <summary>
        /// OnSelectionChange needs to drive changes to the SelectionList Dependency Property
        /// </summary>
        /// <param name="e">The arguments associated with this event</param>
        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
            this.SetValue(SelectionListProperty, this.SelectedItems);
        }
        #endregion // SelectionList Dependency Property
        #endregion // Dependency Properties
    }
}
Loading