<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->
<UserControl x:Class="Microsoft.Windows.Controls.Samples.UpDownBaseSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
xmlns:samples="clr-namespace:Microsoft.Windows.Controls.Samples">
<StackPanel>
<!-- UpDownBase<T> -->
<ContentControl Content="UpDownBase<T>" Style="{StaticResource Header}" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Style="{StaticResource Description}" Text="NUD : UpDownBase<int>"/>
<StackPanel x:Name="spNUD" Grid.Row="1" Grid.Column="0" Margin="5"/>
<TextBlock Grid.Row="0" Grid.Column="1" Style="{StaticResource Description}" Text="TUD : UpDownBase<DateTime>"/>
<StackPanel x:Name="spTUD" Grid.Row="1" Grid.Column="1" Margin="5"/>
<TextBlock Grid.Row="0" Grid.Column="2" Style="{StaticResource Description}" Text="SUD : UpDownBase<string>"/>
<StackPanel x:Name="spSUD" Grid.Row="1" Grid.Column="2" Margin="5"/>
</Grid>
<!-- Interactive Usage -->
<ContentControl Content="Interactive Usage" Style="{StaticResource Header}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid>
<StackPanel x:Name="sp">
<!--
<TextBlock x:Name="output" />
-->
</StackPanel>
</Grid>
<Grid Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Type:" Grid.Row="0" Grid.Column="0" />
<ListBox x:Name="lbType" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Left">
<ListBoxItem Content="NUD : UpDownBase<int>" IsSelected="True"/>
<ListBoxItem Content="TUD : UpDownBase<DateTime>" />
<ListBoxItem Content="SUD : UpDownBase<string>" />
</ListBox>
<TextBlock Text="Value:" Grid.Row="1" Grid.Column="0" />
<TextBox x:Name="tbValue" Grid.Row="1" Grid.Column="2" />
<!--
<TextBlock Text="SpinnerStyle:" Grid.Row="2" Grid.Column="0" />
<TextBox x:Name="tbSpinnerStyle" Grid.Row="2" Grid.Column="2" />
<CheckBox x:Name="cbIsCyclic" Grid.Row="3" Grid.Column="2"
IsChecked="False"
HorizontalAlignment="Left" Content="IsCyclic"/>
-->
<CheckBox x:Name="cbIsEditable" Grid.Row="4" Grid.Column="2"
IsChecked="True"
HorizontalAlignment="Left" Content="IsEditable"/>
<CheckBox x:Name="cbIsEnabled" Grid.Row="5" Grid.Column="2"
IsChecked="True"
HorizontalAlignment="Left" Content="IsEnabled" />
<Button x:Name="btnChange" Grid.Row="6" Grid.Column="2"
HorizontalAlignment="Left" Content="Change!"/>
</Grid>
</Grid>
<src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<src:SourceFile Path="UpDownBaseSample.xaml">
<src:SourceFile.Source>
<sys:String><!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->
<UserControl x:Class="Microsoft.Windows.Controls.Samples.UpDownBaseSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
xmlns:samples="clr-namespace:Microsoft.Windows.Controls.Samples">
<StackPanel>
<!-- UpDownBase&lt;T&gt; -->
<ContentControl Content="UpDownBase&lt;T&gt;" Style="{StaticResource Header}" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Style="{StaticResource Description}" Text="NUD : UpDownBase&lt;int&gt;"/>
<StackPanel x:Name="spNUD" Grid.Row="1" Grid.Column="0" Margin="5"/>
<TextBlock Grid.Row="0" Grid.Column="1" Style="{StaticResource Description}" Text="TUD : UpDownBase&lt;DateTime&gt;"/>
<StackPanel x:Name="spTUD" Grid.Row="1" Grid.Column="1" Margin="5"/>
<TextBlock Grid.Row="0" Grid.Column="2" Style="{StaticResource Description}" Text="SUD : UpDownBase&lt;string&gt;"/>
<StackPanel x:Name="spSUD" Grid.Row="1" Grid.Column="2" Margin="5"/>
</Grid>
<!-- Interactive Usage -->
<ContentControl Content="Interactive Usage" Style="{StaticResource Header}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid>
<StackPanel x:Name="sp">
<!--
<TextBlock x:Name="output" />
-->
</StackPanel>
</Grid>
<Grid Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Type:" Grid.Row="0" Grid.Column="0" />
<ListBox x:Name="lbType" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Left">
<ListBoxItem Content="NUD : UpDownBase&lt;int&gt;" IsSelected="True"/>
<ListBoxItem Content="TUD : UpDownBase&lt;DateTime&gt;" />
<ListBoxItem Content="SUD : UpDownBase&lt;string&gt;" />
</ListBox>
<TextBlock Text="Value:" Grid.Row="1" Grid.Column="0" />
<TextBox x:Name="tbValue" Grid.Row="1" Grid.Column="2" />
<!--
<TextBlock Text="SpinnerStyle:" Grid.Row="2" Grid.Column="0" />
<TextBox x:Name="tbSpinnerStyle" Grid.Row="2" Grid.Column="2" />
<CheckBox x:Name="cbIsCyclic" Grid.Row="3" Grid.Column="2"
IsChecked="False"
HorizontalAlignment="Left" Content="IsCyclic"/>
-->
<CheckBox x:Name="cbIsEditable" Grid.Row="4" Grid.Column="2"
IsChecked="True"
HorizontalAlignment="Left" Content="IsEditable"/>
<CheckBox x:Name="cbIsEnabled" Grid.Row="5" Grid.Column="2"
IsChecked="True"
HorizontalAlignment="Left" Content="IsEnabled" />
<Button x:Name="btnChange" Grid.Row="6" Grid.Column="2"
HorizontalAlignment="Left" Content="Change!"/>
</Grid>
</Grid>
</StackPanel>
</UserControl>
</sys:String>
</src:SourceFile.Source>
</src:SourceFile>
<src:SourceFile Path="UpDownBaseSample.xaml.cs">
<src:SourceFile.Source>
<sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
namespace Microsoft.Windows.Controls.Samples
{
/// <summary>
/// UpDownBase sample page.
/// </summary>
[Sample("UpDownBase", DifficultyLevel.Basic)]
[Category("UpDownBase")]
public partial class UpDownBaseSample : UserControl
{
/// <summary>
/// Initializes a new instance of the UpDownBaseSample class.
/// </summary>
public UpDownBaseSample()
{
InitializeComponent();
// create instance of the test non generic sub classes
spNUD.Children.Add(new NUD());
spTUD.Children.Add(new TUD());
spSUD.Children.Add(new SUD());
btnChange.Click += (s, a) => CreateUDB();
}
/// <summary>
/// This function does following three things:
/// 1. Remove existing interactive UpDownBase control if it exists;
/// 2. Create a new instance of selected UpDownBase subclass type;
/// 3. Add the newly created UpDownBase control to visual tree.
/// </summary>
private void CreateUDB()
{
if (udb != null)
{
Debug.Assert(udb == sp.Children[0], "udb should be sp's only child!");
sp.Children.Remove(udb);
}
switch (lbType.SelectedIndex)
{
case 0:
udb = new NUD();
ConfigUDB((UpDownBase<int>)udb);
break;
case 1:
udb = new TUD();
ConfigUDB((UpDownBase<DateTime>)udb);
break;
case 2:
udb = new SUD();
ConfigUDB((UpDownBase<string>)udb);
break;
}
sp.Children.Add(udb);
}
/// <summary>
/// Configure a UpDownBase&lt;T&gt; control.
/// </summary>
/// <typeparam name="T">Type parameter.</typeparam>
/// <param name="udbt">UpDownBase&lt;T&gt; control.</param>
private void ConfigUDB<T>(UpDownBase<T> udbt)
{
Debug.Assert(udbt != null, "udb should not be null!");
udbt.Value = ParseUtility.ReadValue(tbValue, udbt.Value);
////udbt.IsCyclic = (bool)cbIsCyclic.IsChecked;
udbt.IsEditable = (bool)cbIsEditable.IsChecked;
udbt.IsEnabled = (bool)cbIsEnabled.IsChecked;
}
/// <summary>
/// The interactive UpDownBase control.
/// </summary>
private UpDownBase udb;
#region Non-generic test classes inheriting from UpDownBase<T>
/// <summary>
/// Non-generic test class inheriting from UpDownBase&lt;int&gt;.
/// </summary>
/// <remarks>
/// Value is of type int, in between 0 and 10.
/// IsCyclic decides whether Value will wrap around 0 and 10.
/// OnIncrement and OnDecrement change Value by 1.
/// </remarks>
[TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Pressed", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Focused", GroupName = "FocusStates")]
[TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates")]
[TemplatePart(Name = "TextName", Type = typeof(TextBox))]
[TemplatePart(Name = "SpinnerName", Type = typeof(Spinner))]
[StyleTypedProperty(Property = "SpinnerStyle", StyleTargetType = typeof(Spinner))]
internal class NUD : UpDownBase<int>
{
/// <summary>
/// Private variable to replace IsCyclic property for now removed from UpDownBase&lt;T&gt;.
/// </summary>
private bool IsCyclic = true;
/// <summary>
/// Internal constructor.
/// </summary>
internal NUD()
: base()
{
Value = 0;
}
/// <summary>
/// Called by OnSpin when the spin direction is SpinDirection.Increase.
/// </summary>
protected override void OnIncrement()
{
Value = IsCyclic ? (Value + 1) % 10 : Math.Min(10, Value + 1);
}
/// <summary>
/// Called by OnSpin when the spin direction is SpinDirection.Increase.
/// </summary>
protected override void OnDecrement()
{
Value = IsCyclic ? (Value - 1) % 10 : Math.Max(0, Value - 1);
}
/// <summary>
/// Called by ApplyValue to parse user input.
/// </summary>
/// <param name="text">User input.</param>
/// <returns>Value parsed from user input.</returns>
protected override int ParseValue(string text)
{
return int.Parse(text, CultureInfo.CurrentCulture);
}
/// <summary>
/// Called to render Value for Text template part to display.
/// </summary>
/// <returns>Formatted Value.</returns>
protected override string FormatValue()
{
return Value.ToString(CultureInfo.CurrentCulture);
}
}
/// <summary>
/// Non-generic test class inheriting from UpDownBase&lt;DateTime&gt;.
/// </summary>
/// <remarks>
/// Value is of type DateTime.
/// IsCyclic decides whether Value will wrap around DateTime.MinValue and DateTime.MaxValue.
/// OnIncrement and OnDecrement change Value by one hour.
/// </remarks>
[TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Pressed", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Focused", GroupName = "FocusStates")]
[TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates")]
[TemplatePart(Name = "TextName", Type = typeof(TextBox))]
[TemplatePart(Name = "SpinnerName", Type = typeof(Spinner))]
[StyleTypedProperty(Property = "SpinnerStyle", StyleTargetType = typeof(Spinner))]
internal class TUD : UpDownBase<DateTime>
{
/// <summary>
/// Private variable to replace IsCyclic property for now removed from UpDownBase&lt;T&gt;.
/// </summary>
private bool IsCyclic = true;
/// <summary>
/// Internal constructor.
/// </summary>
internal TUD()
: base()
{
Value = DateTime.Now;
}
/// <summary>
/// Called by OnSpin when the spin direction is SpinDirection.Increase.
/// </summary>
protected override void OnIncrement()
{
try
{
Value = Value.AddHours(1);
}
catch (ArgumentOutOfRangeException)
{
Value = IsCyclic ? DateTime.MinValue : DateTime.MaxValue;
}
}
/// <summary>
/// Called by OnSpin when the spin direction is SpinDirection.Increase.
/// </summary>
protected override void OnDecrement()
{
try
{
Value = Value.AddHours(-1);
}
catch (ArgumentOutOfRangeException)
{
Value = IsCyclic ? DateTime.MaxValue : DateTime.MinValue;
}
}
/// <summary>
/// Called by ApplyValue to parse user input.
/// </summary>
/// <param name="text">User input.</param>
/// <returns>Value parsed from user input.</returns>
protected override DateTime ParseValue(string text)
{
return DateTime.Parse(text, CultureInfo.CurrentCulture);
}
/// <summary>
/// Called to render Value for Text template part to display.
/// </summary>
/// <returns>Formatted Value.</returns>
protected override string FormatValue()
{
return Value.ToShortTimeString();
}
}
/// <summary>
/// Non-generic class for UpDownBase&lt;T&gt; testing only.
/// </summary>
/// <remarks>
/// Value is of type string.
/// OnIncrement and OnDecrement change string length by 1 character.
/// IsCyclic decides whether Value length will cycle through 0 to 10.
/// </remarks>
[TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Pressed", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Focused", GroupName = "FocusStates")]
[TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates")]
[TemplatePart(Name = "TextName", Type = typeof(TextBox))]
[TemplatePart(Name = "SpinnerName", Type = typeof(Spinner))]
[StyleTypedProperty(Property = "SpinnerStyle", StyleTargetType = typeof(Spinner))]
internal class SUD : UpDownBase<string>
{
/// <summary>
/// Private variable to replace IsCyclic property for now removed from UpDownBase&lt;T&gt;.
/// </summary>
private bool IsCyclic = true;
/// <summary>
/// Internal constructor.
/// </summary>
internal SUD()
: base()
{
Value = "0";
}
/// <summary>
/// Called by OnSpin when the spin direction is SpinDirection.Increase.
/// </summary>
protected override void OnIncrement()
{
string value = string.IsNullOrEmpty(Value) ? "0" : Value;
if (value.Length >= 10)
{
if (IsCyclic)
{
value = "0";
}
}
else
{
value = value + value.Length.ToString(CultureInfo.CurrentCulture);
}
Value = value;
}
/// <summary>
/// Called by OnSpin when the spin direction is SpinDirection.Increase.
/// </summary>
protected override void OnDecrement()
{
string value = string.IsNullOrEmpty(Value) ? "0" : Value;
if (value.Length <= 1)
{
if (IsCyclic)
{
value = "0123456789";
}
}
else
{
value = value.Substring(0, value.Length - 1);
}
Value = value;
}
/// <summary>
/// Called by ApplyValue to parse user input.
/// </summary>
/// <param name="text">User input.</param>
/// <returns>Value parsed from user input.</returns>
protected override string ParseValue(string text)
{
if (string.IsNullOrEmpty(text))
{
text = "0";
}
if (text.Length > 10)
{
text = text.Substring(0, 10);
}
return text;
}
/// <summary>
/// Called to render Value for Text template part to display.
/// </summary>
/// <returns>Formatted Value.</returns>
protected override string FormatValue()
{
return string.IsNullOrEmpty(Value) ? "0" : Value;
}
}
#endregion
}
}
</sys:String>
</src:SourceFile.Source>
</src:SourceFile>
</src:SourceViewer>
</StackPanel>
</UserControl>