Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / WPF

C# WPF .NET 4.0 ArrowRepeatButton, NumericUpDown, and TimeCtrl Controls

Rate me:
Please Sign up or sign in to vote.
4.39/5 (11 votes)
14 Oct 2011CPOL16 min read 67.5K   3.4K   16  
User controls for entering either a time or a number within a range.
<UserControl x:Class="UpDownCtrls.TimeCtrl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xmlns:local="clr-namespace:UpDownCtrls"
             x:Name="Root"
             d:DesignHeight="23" d:DesignWidth="120"
             Loaded="Root_Loaded"
             MouseLeftButtonDown="Root_MouseLeftButtonDown"
             MouseRightButtonDown="Root_MouseRightButtonDown"
             IsEnabledChanged="Root_IsEnabledChanged"
             FocusManager.IsFocusScope="True">
<!-- Unfortunately, having the line above "FocusManager.IsFocusScope="True"" causes a problem: the selection in light blue sometimes becomes 
    invisible when right click and menu appears. This looks to me like a Microsoft bug, and I feel light blue selection should always remain
    visible. 
If desperate for selection to always display, remove this line and modify GetTextBoxToFocusOn function, so it no longer uses FocusManager. 
    A simple solution would be to simply traverse all items in TimeCtrls grid until find a item which has focus. The drawback of this solution
    is control will no longer keep in memory last item which had focus. To overcome that, you might have a member variable ‘int LastItemFocused’,
    which keeps track of index of last focused item and use that variable when appropriate (that is no items in TimeCtrls currently have focus). -->
    <UserControl.Resources>
        <ContextMenu x:Key="HiddenContextMenu" Visibility="Hidden"/>
    </UserControl.Resources>
    <Grid>
        <TextBox Name="TextBoxCtrl" VerticalAlignment="Stretch" Padding="2,2,18,2" 
                IsEnabled="{Binding IsEnabled, ElementName=Root}"               
                Foreground="{Binding Foreground, ElementName=Root}" 
                Background="{Binding Background, ElementName=Root}" 
                Height="{Binding ActualHeight, ElementName=Root}"
                Width="{Binding ActualWidth, ElementName=Root}"
                VerticalContentAlignment="{Binding VerticalContentAlignment, ElementName=Root}"
                HorizontalContentAlignment="{Binding  HorizontalContentAlignment, ElementName=Root}" Focusable="False"
                ContextMenu="{StaticResource HiddenContextMenu}"
                PreviewDragEnter="tb_PreviewDrag"
                PreviewDragOver="tb_PreviewDrag">
            <TextBox.CommandBindings>
                <CommandBinding Command="Cut" CanExecute="CommandBinding_CanExecute"/>
                <CommandBinding Command="Paste" CanExecute="CommandBinding_CanExecute"/>
            </TextBox.CommandBindings>
        </TextBox>
        <Grid Name="TimeCtrls" Style="{DynamicResource ResourceKey=TimeCtrlsGridStyle}"/>
        <local:UpDownButtons x:Name="UpDown" HorizontalAlignment="Right" 
                Style="{DynamicResource ResourceKey=UpDownButtonsStyle}" 
                VerticalAlignment="Stretch" IsEnabled="{Binding IsEnabled, ElementName=Root}"
                UpClick="UpDown_UpClick" DownClick="UpDown_DownClick"/>
        <Popup Name="ValidTimesPopup" IsOpen="False" StaysOpen="False" PlacementTarget="{Binding ElementName=TextBoxCtrl}" Placement="Bottom">
            <Border BorderBrush="#FF7F9DB9" BorderThickness="1" Background="{Binding Background, ElementName=Root}">
                <Grid Name="ValidTimesGrid" Background="{Binding Background, ElementName=Root}"/>
            </Border>
        </Popup>
    </Grid>
</UserControl>

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
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions