WPF自定义时间控件

效果:
在这里插入图片描述

一:xaml上控件的实现


<!-- 按钮 网格 -->
            <Grid Grid.Row="0" Grid.Column="3" RenderTransformOrigin="0.492,0.54">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.13*" />
                    <ColumnDefinition Width="1.15*" />
                    <ColumnDefinition Width="1.15*" />
                    <ColumnDefinition Width="2.3*" />
                    <ColumnDefinition Width="0.13*" />
                    <ColumnDefinition Width="2.3*" />
                    <ColumnDefinition Width="1.15*" />
                    <ColumnDefinition Width="1.15*" />
                    <ColumnDefinition Width="0.13*" />
                </Grid.ColumnDefinitions>
                <Button Style="{StaticResource BtnInfoStyle}"  Grid.Row="0" HorizontalAlignment="Left" Grid.Column="2" Content="确认回放"  Width="100" Height="35" Click="BntPlayByTime"  />

                <Grid  Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left">
                    <Border x:Name="border" Style="{StaticResource BtnTimeStyle1}" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="2*"></ColumnDefinition>
                                <ColumnDefinition Width="1*" ></ColumnDefinition>
                                <ColumnDefinition Width="2*"></ColumnDefinition>
                                <ColumnDefinition Width="1*"></ColumnDefinition>
                                <ColumnDefinition Width="2*"></ColumnDefinition>
                                <ColumnDefinition Width="2*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <TextBox Name="textbox_hour" Foreground="White"  BorderThickness="0" TextChanged="numtextboxchanged" SelectionChanged="textbox_hour_SelectionChanged" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Column="0"></TextBox>
                            <TextBlock Grid.Column="1" Foreground="White" VerticalAlignment="Center" Background="#323844"  HorizontalAlignment="Center">:</TextBlock>
                            <TextBox Name="textbox_minute" Foreground="White" BorderThickness="0" TextChanged="numtextboxchanged" SelectionChanged="textbox_hour_SelectionChanged" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"  Grid.Column="2"></TextBox>
                            <TextBlock Grid.Column="3" Foreground="White" VerticalAlignment="Center" Background="#323844"   HorizontalAlignment="Center">:</TextBlock>
                            <TextBox Name="textbox_second" Foreground="White" BorderThickness="0" TextChanged="numtextboxchanged" SelectionChanged="textbox_hour_SelectionChanged" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"  Grid.Column="4"></TextBox>
                            <Grid Grid.Column="5">
                                <Grid.RowDefinitions>
                                    <RowDefinition></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                </Grid.RowDefinitions>
                                <Button Name="button_up" Click="button_up_Click" Grid.Row="0">
                                    <Button.Background>
                                        <ImageBrush ImageSource="Image/jiantouxiangshang.png" Stretch="Fill"/>
                                    </Button.Background>
                                </Button>
                                <Button Name="button_down" Click="button_down_Click" Grid.Row="1" FontSize="5">
                                    <Button.Background>
                                        <ImageBrush ImageSource="Image/jiantouxiangxia.png" Stretch="Fill"/>
                                    </Button.Background>
                                </Button>
                            </Grid>
                        </Grid>
                    </Border>
                </Grid>

二:后台代码实现时分秒上下按钮

#region 业务处理函数
        /// <summary>
        /// 更改选中状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_hour_SelectionChanged(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.TextBox tb = sender as System.Windows.Controls.TextBox;
            if (tb != null)
            {
                Color color = (Color)ColorConverter.ConvertFromString("#323844");
                switch (tb.Name)
                {
                    case "textbox_hour":
                        tb.Background = Brushes.Gray;
                        this.textbox_minute.Background = new SolidColorBrush(color);
                        this.textbox_second.Background = new SolidColorBrush(color);
                        break;
                    case "textbox_minute":
                        tb.Background = Brushes.Gray;
                        this.textbox_hour.Background = new SolidColorBrush(color);
                        this.textbox_second.Background = new SolidColorBrush(color);
                        break;
                    case "textbox_second":
                        tb.Background = Brushes.Gray;
                        this.textbox_hour.Background = new SolidColorBrush(color);
                        this.textbox_minute.Background = new SolidColorBrush(color);
                        break;
                }
            }
        }

        /// <summary>
        /// 向上加时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_up_Click(object sender, RoutedEventArgs e)
        {
            if (this.textbox_hour.Background == Brushes.Gray)
            {
                int temp = System.Int32.Parse(this.textbox_hour.Text);
                temp++;
                if (temp > 24)
                {
                    temp = 0;
                }
                this.textbox_hour.Text = temp.ToString();
            }
            else if (this.textbox_minute.Background == Brushes.Gray)
            {
                int temp = System.Int32.Parse(this.textbox_minute.Text);
                temp++;
                if (temp > 60)
                {
                    temp = 0;
                }
                this.textbox_minute.Text = temp.ToString();
            }
            else if (this.textbox_second.Background == Brushes.Gray)
            {
                int temp = System.Int32.Parse(this.textbox_second.Text);
                temp++;
                if (temp > 60)
                {
                    temp = 0;
                }
                this.textbox_second.Text = temp.ToString();
            }
        }

        /// <summary>
        /// 向下减时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_down_Click(object sender, RoutedEventArgs e)
        {
            if (this.textbox_hour.Background == Brushes.Gray)
            {
                int temp = System.Int32.Parse(this.textbox_hour.Text);
                temp--;
                if (temp < 0)
                {
                    temp = 24;
                }
                this.textbox_hour.Text = temp.ToString();
            }
            else if (this.textbox_minute.Background == Brushes.Gray)
            {
                int temp = System.Int32.Parse(this.textbox_minute.Text);
                temp--;
                if (temp < 0)
                {
                    temp = 60;
                }
                this.textbox_minute.Text = temp.ToString();
            }
            else if (this.textbox_second.Background == Brushes.Gray)
            {
                int temp = System.Int32.Parse(this.textbox_second.Text);
                temp--;
                if (temp < 0)
                {
                    temp = 60;
                }
                this.textbox_second.Text = temp.ToString();
            }
        }

        /// <summary>
        /// 初始化参数设置
        /// </summary>
        private void initParameters()
        {
            string strt = System.DateTime.Now.ToString("HH:mm:ss");
            this.textbox_hour.Text = strt.Split(':')[0];
            this.textbox_minute.Text = strt.Split(':')[1];
            this.textbox_second.Text = strt.Split(':')[2];
            string date = System.DateTime.Now.ToString("yyyy:MM:dd");

        }

        /// <summary>
        /// 数字标准化处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numtextboxchanged(object sender, TextChangedEventArgs e)
        {
            System.Windows.Controls.TextBox tb = sender as System.Windows.Controls.TextBox;
            if (tb != null)
            {
                if ((this.isNum(tb.Text) == false) || (tb.Text.Length > 2))
                {
                    tb.Text = "00";
                    System.Windows.MessageBox.Show("请输入正确的时间!", "警告!");
                    return;
                }
            }
        }

        /// <summary>
        /// 判断是否为数字,是--->true,否--->false
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool isNum(string str)
        {
            bool ret = true;
            foreach (char c in str)
            {
                if ((c < 48) || (c > 57))
                {
                    return false;
                }
            }

            return ret;
        }
        #endregion

三:样式:

<Style x:Key="BtnTimeStyle1" TargetType="Border">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="35"/>
            <Setter Property="Background" Value="#323844"/>
            <Setter Property="CornerRadius" Value="2.5"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="BorderBrush">
                <Setter.Value>
                    <LinearGradientBrush EndPoint=".5,0" StartPoint=".5,1">
                        <GradientStop Color="#FFA3AEB9" Offset="0"/>
                        <GradientStop Color="#FF8399A9" Offset="0.375"/>
                        <GradientStop Color="#FF718597" Offset="0.375"/>
                        <GradientStop Color="#FF617584" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值