C#--工作笔记(时间单个控件UserControls和使用)
2013-12-25 10:23
417 查看
<UserControl x:Class="Health365IIProjectView.DateTimeControl" 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" xmlns:local="clr-namespace:Health365IIProjectView" Name="Self" mc:Ignorable="d" d:DesignHeight="30" d:DesignWidth="150"> <UserControl.Resources> <ResourceDictionary> <!-- Brushes --> <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" /> <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" /> <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" /> <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" /> <SolidColorBrush x:Key="SolidBlack" Color="Black"/> <SolidColorBrush x:Key="SolidSilver" Color="Silver"/> <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1"> <GradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#FFF" Offset="0.0"/> <GradientStop Color="#CCC" Offset="1.0"/> </GradientStopCollection> </GradientBrush.GradientStops> </LinearGradientBrush> <LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1"> <GradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#FFF" Offset="0.0"/> <GradientStop Color="#AAA" Offset="1.0"/> </GradientStopCollection> </GradientBrush.GradientStops> </LinearGradientBrush> <LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1"> <GradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#BBB" Offset="0.0"/> <GradientStop Color="#EEE" Offset="0.1"/> <GradientStop Color="#EEE" Offset="0.9"/> <GradientStop Color="#FFF" Offset="1.0"/> </GradientStopCollection> </GradientBrush.GradientStops> </LinearGradientBrush> <LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1"> <GradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#CCC" Offset="0.0"/> <GradientStop Color="#444" Offset="1.0"/> </GradientStopCollection> </GradientBrush.GradientStops> </LinearGradientBrush> <LinearGradientBrush x:Key="DefaultedBorderBrush" StartPoint="0,0" EndPoint="0,1"> <GradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#777" Offset="0.0"/> <GradientStop Color="#000" Offset="1.0"/> </GradientStopCollection> </GradientBrush.GradientStops> </LinearGradientBrush> <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" /> <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" /> <SolidColorBrush x:Key="LightBorderBrush" Color="#AAA" /> <!-- Miscellaneous Brushes --> <SolidColorBrush x:Key="GlyphBrush" Color="#444" /> <SolidColorBrush x:Key="LightColorBrush" Color="#DDD" /> <local:Int32ToStringConverter x:Key="Int32ToString"/> </ResourceDictionary> </UserControl.Resources> <Grid> <ComboBox Grid.Row="0" Name="DateTimePickerComboBox" DropDownClosed="ComboBox_DropDownClosed" Tag="{Binding ElementName=Self, Path=SelectedDateTimeString, Mode=oneWay}" > <ComboBox.Style> <Style TargetType="ComboBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ComboBox"> <Grid> <ToggleButton Name="ToggleButton" Grid.Column="2" Focusable="false" ClickMode="Press" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> <ToggleButton.Template> <ControlTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="3" BorderThickness="1" Background="{StaticResource NormalBrush}" BorderBrush="{StaticResource NormalBorderBrush}"/> <Border Grid.Column="0" CornerRadius="3,0,0,3" Margin="1" BorderThickness="0,0,1,0" Background="{StaticResource WindowBackgroundBrush}" BorderBrush="{StaticResource NormalBorderBrush}"/> <Path x:Name="Arrow" Grid.Column="1" Fill="{StaticResource GlyphBrush}" Data="M 0 0 L 4 4 L 8 0 Z" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </ToggleButton.Template> </ToggleButton> <TextBox x:Name="ContentSite" Text="{TemplateBinding Tag}" Width="129" Height="31" Margin="0,0,20,0" VerticalAlignment="Center" HorizontalAlignment="Left" MouseDoubleClick="ContentSite_MouseDoubleClick" TextChanged="ContentSite_TextChanged" /> <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"> <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}"> <Border x:Name="DropDownBorder" Background="{StaticResource WindowBackgroundBrush}" BorderThickness="1" BorderBrush="{StaticResource SolidBorderBrush}"/> <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> </ScrollViewer> </Grid> </Popup> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ComboBox.Style> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="5*"/> <ColumnDefinition Width="5*"/> </Grid.ColumnDefinitions> <DatePicker x:Name="DatePickerObj" Grid.Column="0" Width="100" IsTodayHighlighted="True" VerticalContentAlignment="Center"/> <Border Grid.Column="1" CornerRadius="5" BorderBrush="Silver" Padding="2"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="8*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <Grid Name="TimePickerObj"> <Grid.ColumnDefinitions> <ColumnDefinition Width="23"/> <ColumnDefinition Width="5"/> <ColumnDefinition Width="23"/> <ColumnDefinition Width="5"/> <ColumnDefinition Width="23"/> </Grid.ColumnDefinitions> <TextBox Grid.Column="0" Name="TextBox_Hours" GotFocus="TextBox_Hours_GotFocus" BorderBrush="Transparent" VerticalAlignment ="Center" HorizontalContentAlignment="Center"> <TextBox.Text> <Binding ElementName="Self" Path="SelectedHour" Converter="{StaticResource Int32ToString}" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:TimeValidationRule MaxValue="23"/> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> <TextBlock Grid.Column="1" Text=":" VerticalAlignment ="Center"/> <TextBox Grid.Column="2" Name="TextBox_Minutes" GotFocus="TextBox_Minutes_GotFocus" BorderBrush="Transparent" VerticalAlignment ="Center" HorizontalContentAlignment="Center"> <TextBox.Text> <Binding ElementName="Self" Path="SelectedMinute" Converter="{StaticResource Int32ToString}" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:TimeValidationRule/> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> <TextBlock Grid.Column="3" Text=":" VerticalAlignment ="Center"/> <TextBox Grid.Column="4" Name="TextBox_Seconds" GotFocus="TextBox_Seconds_GotFocus" BorderBrush="Transparent" VerticalAlignment ="Center" HorizontalContentAlignment="Center"> <TextBox.Text> <Binding ElementName="Self" Path="SelectedSecond" Converter="{StaticResource Int32ToString}" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:TimeValidationRule/> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> </Grid> <Grid Grid.Column="1" Height="20" Width="20" VerticalAlignment ="Center"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Button Name="BTN_IncreaseTime" Grid.Row="0" Height="10" Click="BTN_IncreaseTime_Click"> <Button.Template> <ControlTemplate TargetType="Button"> <Path Name="path" Fill="Silver" Data="M 5,10 L 15,10 L 10,5 L 5,10" Width="10" Height="8" Stretch="Fill"/> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="path" Property="Fill" Value="Black"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Button.Template> </Button> <Button Name="BTN_DecrementTime" Grid.Row="1" Height="10" Click="BTN_DecrementTime_Click"> <Button.Template> <ControlTemplate TargetType="Button"> <Path Name="path" Fill="Silver" Data="M 5,5 L 10,10 L 15,5 L 5,5" Width="10" Height="8" Stretch="Fill"/> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="path" Property="Fill" Value="Black"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Button.Template> </Button> </Grid> </Grid> </Border> </Grid> </ComboBox> </Grid> </UserControl>
================================================================================
using System; using System.Collections.Generic; using System.Linq; using System.Text; 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 System.ComponentModel; namespace Health365IIProjectView { /// <summary> /// DateTimeControl.xaml 的交互逻辑 /// </summary> public partial class DateTimeControl : UserControl,INotifyPropertyChanged { public bool fir = true; public DateTimeControl() { InitializeComponent(); DatePickerObj.SelectedDate = DateTime.Now; _selectedHour = DateTime.Now.Hour; _selectedMinute = DateTime.Now.Minute; _selectedSecond = 0; _oldTextBox = _currentTextBox = TextBox_Seconds; _currentTextBox.Background = Brushes.Pink; } private void ContentSite_MouseDoubleClick(object sender, MouseButtonEventArgs e) { fir = true; this.SendPropertyChanged("SelectedDateTimeString"); } public DateTime SelectedDateTime { get { DateTime obj = ((DatePickerObj == null) ? DateTime.Now : DatePickerObj.SelectedDate ?? DateTime.Now); return obj.Add(new TimeSpan(SelectedHour - obj.Hour, SelectedMinute - obj.Minute, SelectedSecond - obj.Second)); } } public string SelectedDateTimesFromTextbox=""; public String SelectedDateTimeString { get { if (fir == true) { return ""; } else { return SelectedDateTime.ToString("yyyy-MM-dd HH:mm:ss"); } } //set //{ // SelectedDateTime.ToString("0000-00-00 00:00:00"); //} } #region private methods private void TextBox_Hours_GotFocus(object sender, RoutedEventArgs e) { _oldTextBox.Background = Brushes.Transparent; _oldTextBox = _currentTextBox = TextBox_Hours; _currentTextBox.Background = Brushes.Pink; } private void TextBox_Minutes_GotFocus(object sender, RoutedEventArgs e) { _oldTextBox.Background = Brushes.Transparent; _oldTextBox = _currentTextBox = TextBox_Minutes; _currentTextBox.Background = Brushes.Pink; } private void TextBox_Seconds_GotFocus(object sender, RoutedEventArgs e) { _oldTextBox.Background = Brushes.Transparent; _oldTextBox = _currentTextBox = TextBox_Seconds; _currentTextBox.Background = Brushes.Pink; } private void ComboBox_DropDownClosed(object sender, EventArgs e) { fir = false; this.SendPropertyChanged("SelectedDateTime"); this.SendPropertyChanged("SelectedDateTimeString"); } private void BTN_IncreaseTime_Click(object sender, RoutedEventArgs e) { Int32 result; if (Int32.TryParse(_currentTextBox.Text, out result)) { result++; _currentTextBox.Text = result.ToString(); } } private void BTN_DecrementTime_Click(object sender, RoutedEventArgs e) { Int32 result; if (Int32.TryParse(_currentTextBox.Text, out result)) { result--; _currentTextBox.Text = result.ToString(); } } #endregion #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void SendPropertyChanged(String propertyName) { if (PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } #endregion #region private area public Int32 SelectedHour { get { return _selectedHour; } set { _selectedHour = value; SendPropertyChanged("SelectedHour"); } } public Int32 SelectedMinute { get { return _selectedMinute; } set { _selectedMinute = value; SendPropertyChanged("SelectedMinute"); } } public Int32 SelectedSecond { get { return _selectedSecond; } set { _selectedSecond = value; SendPropertyChanged("SelectedSecond"); } } private TextBox _currentTextBox, _oldTextBox; private Int32 _selectedHour = DateTime.Now.Hour; private Int32 _selectedMinute = DateTime.Now.Minute; private Int32 _selectedSecond = DateTime.Now.Second; #endregion private void ContentSite_TextChanged(object sender, TextChangedEventArgs e) { SelectedDateTimesFromTextbox = ((TextBox)sender).Text; } } public class Int32ToStringConverter : IValueConverter { object IValueConverter.Convert(object obj, Type type, object parameter, System.Globalization.CultureInfo culture) { return obj.ToString(); } object IValueConverter.ConvertBack(object obj, Type type, object parameter, System.Globalization.CultureInfo culture) { return Int32.Parse((String)obj); } } public class TimeValidationRule : ValidationRule { public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { Int32 iValue; if (Int32.TryParse((String)value, out iValue)) { if (iValue < _minValue || iValue > _maxValue) return new ValidationResult(false, "Out of range!"); else return new ValidationResult(true, null); } else { return new ValidationResult(false, null); } } public Int32 MinValue { get { return _minValue; } set { _minValue = value; } } public Int32 MaxValue { get { return _maxValue; } set { _maxValue = value; } } private Int32 _minValue = 0; private Int32 _maxValue = 59; } }
总结:加载时是空的,支持双击和回格的事件
=================================
调用:
xmlns:Ctrl="clr-namespace:Health365IIProjectView" xmlns:my="clr-namespace:Health365IIProjectView" <Ctrl:DateTimeControl x:Name="dateTimeControlstart" Width="150" Height="35" Margin="5,1,5,5"/> <my:DateTimeControl x:Name="dateTimeControlend" Width="150" Height="35" Margin="5,1,5,5" />
string _startTime = this.dateTimeControlstart.SelectedDateTimesFromTextbox; string _endTime = this.dateTimeControlend.SelectedDateTimesFromTextbox; if (_startTime.Equals(_endTime)) { start = "1970-1-1 00:00:00"; end = "1970-1-1 00:00:00"; } else { start = this.dateTimeControlstart.SelectedDateTimesFromTextbox; end = this.dateTimeControlend.SelectedDateTimesFromTextbox; }
相关文章推荐
- C#--工作笔记(直方图单个UserControls)
- Android笔记--仿拖动选择日期时间控件的单个item
- C#--工作笔记(直方图整体UserControls)
- 工作笔记---js时间插件的使用
- web前台工作笔记(时间戳、js中clone的使用、js中动态填充数据注意事项、前台查错方法的学习)
- Android笔记--仿拖动选择日期时间控件的单个item
- 使用IE WebControls中的TabStrip控件和MultiPage控件实现选项卡式风格页面
- 近几天使用到多张表合成一张表、时间转化笔记。
- my97时间控件使用
- Delphi FireMonkey 学习笔记 – TPopup 控件的使用
- 《第一行代码》学习笔记-- 基础的自定义标题控件的定义和使用
- 使用easyui控件中的easyui-datetimebox时间控件,获取时间值的方法
- iOS学习笔记1--在xcode6以上的版本中不使用storyboard以及部分控件使用
- Telerik公司的RadControls控件学习笔记
- robotframe使用之时间控件
- SharePoint2010:从用户控件中打开文档 -Open document(s) in Office web apps (from User controls)
- Android日期时间控件的学习笔记
- C++windows内核编程笔记day09_day10,对话框和窗口基本控件等的使用
- 工作笔记_使用EasyUI Tree异步加载JSON数据 生成树
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.9.Progressbar控件