您的位置:首页 > 编程语言 > C#

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: