WPF自定义时间选择控件

 


    <UserControl.Resources>
        <Style TargetType="DatePickerTextBox">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox Text="{Binding Path=SelectedDate,UpdateSourceTrigger=PropertyChanged, StringFormat='yyyy/MM/dd HH:mm:ss', RelativeSource={RelativeSource AncestorType=DatePicker}}" BorderThickness="0" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
		
    </UserControl.Resources>
	
                                <StackPanel Orientation="Horizontal" Margin="5" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4">
                                    <Label Content="{DynamicResource From:}" MinWidth="50" HorizontalContentAlignment="Right"></Label>
                                    <DatePicker Name="dtp" Margin="5,0" Text="{Binding DateFrom,UpdateSourceTrigger=PropertyChanged}" MinWidth="160"></DatePicker>
                                    <Label Content="{DynamicResource To:}" MinWidth="50" HorizontalContentAlignment="Right"></Label>
                                    <DatePicker  Margin="5,0" Text="{Binding DateTo,UpdateSourceTrigger=PropertyChanged}" MinWidth="160"></DatePicker>
                                </StackPanel>
		

                                <RadioButton Content="{DynamicResource Last month}" IsChecked="{Binding IsLastMonth}" Grid.Row="1" Grid.Column="0" Margin="5" ></RadioButton>
                                <RadioButton Content="{DynamicResource Last week}"  IsChecked="{Binding IsLastWeek}" Grid.Row="1" Grid.Column="1"  Margin="5" ></RadioButton>
                                <RadioButton Content="{DynamicResource Yesterday}" IsChecked="{Binding IsYesterday}" Grid.Row="1" Grid.Column="2" Margin="5" ></RadioButton>

                                <RadioButton Content="{DynamicResource Today}"  IsChecked="{Binding IsToday}" Grid.Row="2" Grid.Column="0" Margin="5" ></RadioButton>
								
								

 


        private string _DateFrom = DateTime.Now.AddDays(-1).ToString();

        public string DateFrom
        {
            get { return _DateFrom; }
            set
            {
                _DateFrom = value;
                RaisePropertyChanged("DateFrom");
            }
        }


        private string _DateTo = DateTime.Now.ToString();

        public string DateTo
        {
            get { return _DateTo; }
            set
            {
                _DateTo = value;

                DateTime dt = Convert.ToDateTime(value);
                dt = new DateTime(dt.Year, dt.Month, dt.Day);
                int sec = (24 * 60) * 60 - 1;
                string tem = dt.AddSeconds(sec).ToString();
                if (_DateTo.EndsWith("00:00:00"))
                    DateTo = tem;

                RaisePropertyChanged("DateTo");
            }
        }


        private bool _IsLastMonth;

        public bool IsLastMonth
        {
            get { return _IsLastMonth; }
            set
            {
                _IsLastMonth = value;
                if (value)
                {
                    //GregorianCalendar gc = new GregorianCalendar();
                    //PersianDate pd = new PersianDate(DateTime.Now);

                    DateTime dt = DateTime.Now;
                    dt = new DateTime(dt.Year, dt.Month, dt.Day);
                    dt = dt.AddDays(-dt.Day);

                    int days = dt.Day - 1;

                    DateFrom = dt.AddDays(-days).ToString();
                    int sec = (24 * 60) * 60 - 1;
                    DateTo = dt.AddSeconds(sec).ToString();

                }
                RaisePropertyChanged("IsLastMonth");
            }
        }


        private bool _IsLastWeek;

        public bool IsLastWeek
        {
            get { return _IsLastWeek; }
            set
            {
                _IsLastWeek = value;

                if (value)
                {
                    DateTime dt = DateTime.Now;
                    dt = new DateTime(dt.Year, dt.Month, dt.Day);
                    dt = dt.AddDays(-(int)dt.DayOfWeek - 1);
                    DateFrom = dt.AddDays(-6).ToString();

                    int sec = (24 * 60) * 60 - 1;
                    DateTo = dt.AddSeconds(sec).ToString();

                }
                RaisePropertyChanged("IsLastWeek");
            }
        }


        private bool _IsYesterday;

        public bool IsYesterday
        {
            get { return _IsYesterday; }
            set
            {
                _IsYesterday = value;
                if (value)
                {
                    DateTime dt = DateTime.Now;
                    dt = new DateTime(dt.Year, dt.Month, dt.Day);
                    dt = dt.AddDays(-1);

                    DateFrom = dt.ToString();

                    int sec = (24 * 60) * 60 - 1;
                    DateTo = dt.AddSeconds(sec).ToString();
                }
                RaisePropertyChanged("IsYesterday");
            }
        }


        private bool _IsToday;

        public bool IsToday
        {
            get { return _IsToday; }
            set
            {
                _IsToday = value;
                if (value)
                {
                    DateTime dt = DateTime.Now;
                    dt = new DateTime(dt.Year, dt.Month, dt.Day);

                    DateFrom = dt.ToString();

                    int sec = (24 * 60) * 60 - 1;
                    DateTo = dt.AddSeconds(sec).ToString();
                }
                RaisePropertyChanged("IsToday");
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@David Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值