欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF自定义时间控件

程序员文章站 2022-06-07 10:49:13
...

效果:
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>
相关标签: WPF学习记录 wpf