一、TimePicker
WPF 没有自带的时间选择器
1.1 xaml
由三个Combox(时、分、秒)和两个TextBlock(::)组成
<UserControl x:Class="lyrics.ui.Controls.TimePickerCustom.TimePicker"
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:lyrics.ui.Controls.TimePickerCustom"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="250" Background="White">
<Grid>
<StackPanel Orientation="Horizontal">
<ComboBox Name="hoursCb" MinWidth="80" ItemsSource="{Binding hours}" SelectedIndex="0" VerticalContentAlignment="Center" MinHeight="50" SelectionChanged="hoursChanged"/>
<TextBlock Text=":" VerticalAlignment="Center"/>
<ComboBox Name="minutesCb" MinWidth="80" ItemsSource="{Binding minutes}" SelectedIndex="0" VerticalContentAlignment="Center" MinHeight="50" SelectionChanged="minutesChanged"/>
<TextBlock Text=":" VerticalAlignment="Center"/>
<ComboBox Name="secondsCb" MinWidth="80" ItemsSource="{Binding seconds}" SelectedIndex="0" VerticalContentAlignment="Center" MinHeight="50" SelectionChanged="secondsChanged"/>
</StackPanel>
</Grid>
</UserControl>
1.2 后台
提供时间改变的事件
using System;
using System.Collections.Generic;
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;
namespace lyrics.ui.Controls.TimePickerCustom
{
/// <summary>
/// TimePicker.xaml 的交互逻辑
/// </summary>
public partial class TimePicker : UserControl
{
public List<int> hours { get; set; } = new List<int>();
public List<int> minutes { get; set; } = new List<int>();
public List<int> seconds { get; set; } = new List<int>();
public int hour { get; set; } = 0;
public int minute { get; set; } = 0;
public int second { get; set; } = 0;
public TimeSpan time { get; set; }
public static readonly RoutedEvent TimeChangedEvent = EventManager.RegisterRoutedEvent("TimeChanged", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(TimePicker));
public event RoutedEventHandler TimeChanged
{
add
{
AddHandler(TimeChangedEvent, value);
}
remove
{
RemoveHandler(TimeChangedEvent, value);
}
}
public TimePicker()
{
for (int i = 0; i < 24; i++) {
hours.Add(i);
}
for (int i = 0; i < 60; i++)
{
minutes.Add(i);
seconds.Add(i);
}
InitializeComponent();
this.DataContext = this;
}
private void hoursChanged(object sender, SelectionChangedEventArgs e)
{
hour = hoursCb.SelectedIndex;
time = new TimeSpan(hour,minute,second);
NotifyEvent();
}
private void minutesChanged(object sender, SelectionChangedEventArgs e)
{
minute = minutesCb.SelectedIndex;
time = new TimeSpan(hour, minute, second);
NotifyEvent();
}
private void secondsChanged(object sender, SelectionChangedEventArgs e)
{
second = secondsCb.SelectedIndex;
time = new TimeSpan(hour, minute, second);
NotifyEvent();
}
private void NotifyEvent() {
if (!IsLoaded) {
return;
}
RoutedEventArgs routedEvent = new RoutedEventArgs();
routedEvent.RoutedEvent = TimeChangedEvent;
RaiseEvent(routedEvent);
}
}
}
二、使用
2.1 Demo
xaml只需要这一行
<tp:TimePicker x:Name="tp" Height="50" TimeChanged="tp_TimeChanged"/>
后台事件
private void tp_TimeChanged(object sender, RoutedEventArgs e)
{
MessageBox.Show("已经设置时间为:"+tp.time.ToString());
}
2.2 效果
控件刷新被弹窗阻,需要点击弹窗才会刷新
三、修改样式
3.1 Combox样式修改
<Style x:Key="ComboBoxReadonlyToggleButton2" TargetType="ToggleButton">
<Setter Property="IsTabStop" Value="false" />
<Setter Property="ClickMode" Value="Press" />
<Setter Property="MinHeight" Value="22"></Setter>
<Setter Property="MinWidth" Value="80"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid>
<Border x:Name="Background" BorderBrush="#787878"
BorderThickness="1" Background="Black">
</Border>
<!--倒三角的颜色#0099ff-->
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!--移入时按钮颜色 图案颜色 背景颜色 锯齿颜色-->
<Setter TargetName="Background" Property="Opacity" Value="0.4"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ComboBoxStyle3" TargetType="ComboBox">
<Setter Property="Foreground" Value="#FFFFFFFF" />
<Setter Property="Width" Value="50"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Left"></Setter>
<Setter Property="BorderBrush" Value="#FFFFFFFF" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="Padding" Value="4,3" />
<Setter Property="FontSize" Value="18" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<!--这个是下拉框的属性设置-->
<Style TargetType="ComboBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Border Name="Back" Background="#525252" BorderThickness="0,0,0,0" BorderBrush="#525252" >
<ContentPresenter ContentSource="{Binding Source}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0" ></ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Back" Property="Background" Value="#484848"></Setter>
</Trigger>
<!--下拉框背景色-->
<Trigger Property="IsHighlighted" Value="True">
<Setter TargetName="Back" Property="Background" Value="Gray"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid x:Name="MainGrid">
<Popup x:Name="PART_Popup" Margin="1" Grid.Row="1"
IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}">
<Grid x:Name="Shdw" MaxHeight="{TemplateBinding MaxDropDownHeight}"
MinWidth="{Binding Path=ActualWidth, ElementName=MainGrid}" >
<!--下拉框的属性设置-->
<!--Background -->
<Border x:Name="Bordertop" Width="60" CornerRadius="0" BorderThickness="0" Background="#525252">
<Border.Effect>
<DropShadowEffect Color="Black" BlurRadius="2" ShadowDepth="0" Opacity="1"/>
</Border.Effect>
<ScrollViewer Width="150">
<ItemsPresenter/>
</ScrollViewer>
</Border>
</Grid>
</Popup>
<ToggleButton Style="{StaticResource ComboBoxReadonlyToggleButton2}" Opacity="0"
BorderThickness="3" BorderBrush="#03ffea" Background="{TemplateBinding Background}"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
<!--combox字体的间距-->
<TextBlock Margin="10 0 0 0"
FontSize="20"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
IsHitTestVisible="false"
Foreground="White"
Text="{TemplateBinding Text}"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
3.2 修改Xaml
<UserControl x:Class="lyrics.ui.Controls.TimePickerCustom.TimePicker"
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:lyrics.ui.Controls.TimePickerCustom"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="150" Background="Transparent">
<Grid>
<Border Background="Black" CornerRadius="25">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<ComboBox Name="hoursCb" MinWidth="40" ItemsSource="{Binding hours}" SelectedIndex="0" VerticalContentAlignment="Center" MinHeight="50" SelectionChanged="hoursChanged" Style="{StaticResource ComboBoxStyle3}" HorizontalContentAlignment="Center"/>
<TextBlock Text=":" VerticalAlignment="Center" FontSize="50" Foreground="White"/>
<ComboBox Name="minutesCb" MinWidth="40" ItemsSource="{Binding minutes}" SelectedIndex="0" VerticalContentAlignment="Center" MinHeight="50" SelectionChanged="minutesChanged"
Style="{StaticResource ComboBoxStyle3}" HorizontalContentAlignment="Center"/>
<TextBlock Text=":" VerticalAlignment="Center" FontSize="50" Foreground="White"/>
<ComboBox Name="secondsCb" MinWidth="40" ItemsSource="{Binding seconds}" SelectedIndex="0" VerticalContentAlignment="Center" MinHeight="50" SelectionChanged="secondsChanged"
Style="{StaticResource ComboBoxStyle3}" HorizontalContentAlignment="Center"/>
</StackPanel>
</Border>
</Grid>
</UserControl>
3.3 修改调用
<tp:TimePicker x:Name="tp" Height="50" Width="200" TimeChanged="tp_TimeChanged" />
3.4 效果
默认:
修改时间
选中
修改事件触发