当我点击按钮时弹出窗口中的datepicker直接打开?

时间:2014-03-06 10:22:20

标签: windows-phone-8 datepicker timepicker

作为described我在我的xaml文件中输入 datepicker

当我运行页面时,datepicker只显示如下:

enter image description here

然后我必须点击日期选择器进入选择页面,如下所示:

enter image description here

现在

我点击按钮时需要直接打开全屏幕日期选择器选择页

地址提供了一种方法,我可以导航到选择页面,

但我不知道怎么办?

1 个答案:

答案 0 :(得分:2)

我是海报。

我自己找到了解决方案

使用我们的自定义DatePickerCustom类覆盖DatePicker类。创建新类“DatePickerCustom.cs”

public class DatePickerCustom : DatePicker
{
    public void ClickTemplateButton()
    {
        Button btn = (GetTemplateChild("DateTimeButton") as Button);
        ButtonAutomationPeer peer = new ButtonAutomationPeer(btn);
        IInvokeProvider provider = (peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider);

        provider.Invoke();
    }
}

然后在mainpage.xaml.cs

private DatePickerCustom datePicker;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        // create datePicker programmatically
        if (this.datePicker == null)
        {
            this.datePicker = new DatePickerCustom();
            this.datePicker.IsTabStop = false;
            this.datePicker.MaxHeight = 0;

            this.datePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(datePicker_ValueChanged);

            LayoutRoot.Children.Add(this.datePicker);

        }
    }

    void datePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
    {
        // now we may use got value from datePicker
        TextBlock1.Text = this.datePicker.ValueString;
    }

这样当点击或点击操作时,将显示全屏日期选择器页面

private void button1_Click(object sender, RoutedEventArgs e)
{
     this.datePicker.ClickTemplateButton();
}

ps:timepicker也可以做同样的事情

ps2:here是细节

@MarioGalván

希望对你有所帮助