[U8.1] DatePicker和TimePicker设置默认字符串 [英] [U8.1] DatePicker and TimePicker setting default string

查看:82
本文介绍了[U8.1] DatePicker和TimePicker设置默认字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


默认情况下,UWA 8.1 for Windows Store和Windows Phone(WinRT)中的DatePicker和TimePicker显示今天的日期和当前时间,其中我想将最初的控件设置为某个默认字符串让我们说" ;设置到期日期/时间"因为它们是
可选。

DatePicker and TimePicker in UWA 8.1 for Windows Store and Windows Phone (WinRT) by default it shows today date and current time, where as I want to set initially both the controls to some default string let's say "Set Due Date/Time" because they're optional.


我只使用MVVM模式和MVVM光库将它们绑定到以下属性

I am binding them to the following properties using MVVM pattern and MVVM light libraries only

public DateTimeOffset? DueDate { get; set; }

public TimeSpan? DueTime { get; set; }





我已经尝试过以下方式,如果有任何其他选择请建议。

I've tried in the following way, if any other alternatives please suggest.


我使用了两个转换器但是有一个问题,当我点击"Set"按钮时截止日期"然后单击日期选择器(弹出/选择屏幕)中的刻度线而不更改转换器不会触发的日期,月份或年份,当我更改
到日期,月份或年份然后它会触发,我看到了即使属性绑定是可为空的DateTimeOffset,XAML中的DatePicker显示Date值到当前日期,IsHitTestVisible =" False"移除倾斜效果,这是很好的,因为
按钮无法再点击,但倾斜效果对我来说不是必须的选项

I have used two converters but there is a problem, when I click the button that says "Set Due Date" and click the tick mark in the datepicker (popup/selection screen) without changing date, month or year the converter doesn't fire, when I make a change to date, month or year then it fires, I see that even if the property bound is a nullable DateTimeOffset the DatePicker in XAML shows Date value to current date, also IsHitTestVisible="False" removes the tilt effect which is nice to have, because the button can't be clicked anymore, but the tilt effect is not a must have option for me

<Page
    x:Class="UWP.MVVM.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWP.MVVM"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="using:UWP.MVVM.ViewModels"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:converters="using:UWP.MVVM.Converters"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
        <converters:DateTimeOffsetToVisibilityConverter x:Key="DateTimeOffsetToVisibilityConverter"/>
        <converters:DateTimeOffsetToOpacityConverter x:Key="DateTimeOffsetToOpacityConverter"/>
    </Page.Resources>

    <Grid Margin="24,24">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBox Header="First Name" 
                 Text="{Binding Person.FirstName}"/>
        <DatePicker Name="DateOfBirth"
                    Date="{Binding Person.DateOfBirth, Mode=TwoWay}"
                    Grid.Row="1"
                    Opacity="{Binding Person.DateOfBirth, Converter={StaticResource DateTimeOffsetToOpacityConverter}}"/>
        <Button Grid.Row="1"
                Content="Set Due Date"
                IsHitTestVisible="False"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                HorizontalContentAlignment="Left"
                Visibility="{Binding Person.DateOfBirth, Converter={StaticResource DateTimeOffsetToVisibilityConverter}}"/>
    </Grid>
</Page>

namespace UWP.MVVM.Converters
{
    using System;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Data;

    public class DateTimeOffsetToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (value == Visibility.Visible)
            {
                return null;
            }
            else
            {
                return DateTimeOffset.Now;
            }
        }
    }
}

namespace UWP.MVVM.Converters
{
    using System;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Data;

    public class DateTimeOffsetToOpacityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
            {
                return 0D;
            }
            else
            {
                return 1D;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (value == 0D)
            {
                return null;
            }
            else
            {
                return DateTimeOffset.Now;
            }
        }
    }
}













推荐答案

Hi MohammedAbdulMateen,

Hi MohammedAbdulMateen,

欢迎使用
来开发通用Windows应用程序论坛!

请阅读粘贴帖子,尤其是

发布指南:主题行标签

Windows 10 SDK和工具的已知问题 

Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools 

根据您的代码段,我无法重现您的问题。因为我没有看到你如何在按钮点击命令处理程序中设置"截止日期"。

Based on your code snippet, I could not reproduce your issue. Because I have not saw that you how to set "Due Date" in button click command handler.

但我已经制作了一个代码样本进行测试,它运行良好。请参考我的以下代码来检查您的代码:

But I have made a code sample to test, it worked well. Please refer to my following code to check your code:

<Grid Margin="24,24">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBox Header="First Name" 
                 Text="{Binding person.FirstName}"/>
        <DatePicker Name="DateOfBirth"
                    Date="{Binding person.DateOfBirth, Mode=TwoWay}"
                    Grid.Row="1"
                    Opacity="{Binding person.DateOfBirth, Converter={StaticResource DateTimeOffsetToOpacityConverter}}"/>
        <Button Grid.Row="2"
                Content="Set Due Date"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                HorizontalContentAlignment="Left"
                Visibility="{Binding person.DateOfBirth, Converter={StaticResource DateTimeOffsetToVisibilityConverter}}">
            <interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="Click">
                    <core:InvokeCommandAction Command="{Binding ButtonClick}"/>
                </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
        </Button>
        <Button Content="Click Me!"
                Grid.Row="3">
            <interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="Click">
                    <core:InvokeCommandAction Command="{Binding ButtonClick}"/>
                </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
        </Button>
    </Grid>


public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            person = new Person() {FirstName="test1" };
            ButtonClick = new RelayCommand(ButtonHandler);
        }

        private Person _person;
        public Person person
        {
            get { return _person; }
            set
            {
                _person = value;
                RaisePropertyChanged("person");
            }
        }

        public RelayCommand ButtonClick { get; set; }

        protected void ButtonHandler()
        {
            Debug.WriteLine("button click");
            person.DateOfBirth = DateTime.Now;
        }
    }


public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            person = new Person() {FirstName="test1" };
            ButtonClick = new RelayCommand(ButtonHandler);
        }

        private Person _person;
        public Person person
        {
            get { return _person; }
            set
            {
                _person = value;
                RaisePropertyChanged("person");
            }
        }

        public RelayCommand ButtonClick { get; set; }

        protected void ButtonHandler()
        {
            Debug.WriteLine("button click");
            person.DateOfBirth = DateTime.Now;
        }
    }


public class Person:ViewModelBase
    {
        private string _FirstName;
        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                _FirstName = value;
                RaisePropertyChanged("FirstName");
            }
        }

        private DateTimeOffset? _DateOfBirth;
        public DateTimeOffset? DateOfBirth
        {
            get { return _DateOfBirth; }
            set
            {
                _DateOfBirth = value;
                RaisePropertyChanged("DateOfBirth");
            }
        }
    }


public MainPage()
        {
            this.InitializeComponent();
            this.DataContext = new MainViewModel();
        }

最好的问候,

Xavier Eoro





Xavier Eoro



这篇关于[U8.1] DatePicker和TimePicker设置默认字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆