C#/XAML Metro应用程序中的日期选择器控件?
我想在我的metro应用程序中使用DATEPICKER控件,但在c#/xaml metro应用程序中似乎没有可用的DATEPICKER控件 你们有没有人找到解决这个问题的办法 请让我知道C#/XAML Metro应用程序中的日期选择器控件?,xaml,microsoft-metro,winrt-xaml,Xaml,Microsoft Metro,Winrt Xaml,我想在我的metro应用程序中使用DATEPICKER控件,但在c#/xaml metro应用程序中似乎没有可用的DATEPICKER控件 你们有没有人找到解决这个问题的办法 请让我知道 谢谢和问候。从提供日期选择器。但是它不是免费的。仔细看看,我想这就是你要找的 在此之前,您必须安装免费windows8 rc我在codeplex上的一个私人解决方案中发现了这一点,我真诚地向拥有该链接的人道歉。。。但您可以使用以下代码创建控件: Xaml文件: <UserControl x:Class="
谢谢和问候。从提供日期选择器。但是它不是免费的。仔细看看,我想这就是你要找的
在此之前,您必须安装免费windows8 rc我在codeplex上的一个私人解决方案中发现了这一点,我真诚地向拥有该链接的人道歉。。。但您可以使用以下代码创建控件: Xaml文件:
<UserControl x:Class="MyNamespace.Controls.DatePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ComboBox x:Name="Day" Grid.Column="0" SelectionChanged="Day_SelectionChanged" Width="70" Margin="0,0,10,0" />
<ComboBox x:Name="Month" Grid.Column="1" SelectionChanged="Day_SelectionChanged" Width="70" Margin="0,0,10,0" />
<ComboBox x:Name="Year" Grid.Column="2" SelectionChanged="Day_SelectionChanged" Width="100" />
</Grid>
</Grid>
</UserControl>
其背后的代码是:
using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace MyNamespace.Controls
{
public sealed partial class DatePicker
{
private bool initializing = true;
public DatePicker()
{
InitializeComponent();
UpdateValues(0, 0);
Day.SelectedIndex = 0;
Month.SelectedIndex = 0;
Year.SelectedIndex = 0;
initializing = false;
}
public static readonly DependencyProperty AllowNullProperty =
DependencyProperty.Register("AllowNull", typeof(bool), typeof(DatePicker), new PropertyMetadata(true));
public bool AllowNull
{
get { return (bool)GetValue(AllowNullProperty); }
set { SetValue(AllowNullProperty, value); }
}
public static readonly DependencyProperty SelectedDateProperty =
DependencyProperty.Register("SelectedDate", typeof(DateTime?), typeof(DatePicker), new PropertyMetadata(null, OnSelectedItemChanged));
public DateTime? SelectedDate
{
get { return (DateTime?)GetValue(SelectedDateProperty); }
set { SetValue(SelectedDateProperty, value); }
}
public event RoutedEventHandler SelectedItemChanged;
private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = (DatePicker)d;
if (ctrl.initializing)
return;
ctrl.initializing = true;
ctrl.UpdateDate();
ctrl.initializing = false;
if (ctrl.SelectedItemChanged != null)
ctrl.SelectedItemChanged(ctrl, new RoutedEventArgs());
}
public void UpdateDate()
{
if (SelectedDate.HasValue)
{
UpdateValues(SelectedDate.Value.Year, SelectedDate.Value.Month);
if (AllowNull)
{
Day.SelectedIndex = SelectedDate.Value.Day;
Month.SelectedIndex = SelectedDate.Value.Month;
Year.SelectedIndex = SelectedDate.Value.Year - 2000 + 1;
}
else
{
Day.SelectedIndex = SelectedDate.Value.Day - 1;
Month.SelectedIndex = SelectedDate.Value.Month - 1;
Year.SelectedIndex = SelectedDate.Value.Year - 2000;
}
}
else
{
UpdateValues(0, 0);
Day.SelectedIndex = 0;
Month.SelectedIndex = 0;
Year.SelectedIndex = 0;
}
}
public void UpdateValues(int year, int month)
{
var days = new List<string>();
if (AllowNull)
days.Add(" ");
for (var i = 1; i <= 31; i++)//(year != 0 && month != 0 ? DateTime.DaysInMonth(year, month) : 31); i++)
days.Add(i.ToString());
var months = new List<string>();
if (AllowNull)
months.Add(" ");
for (var i = 1; i <= 12; i++)
months.Add(i.ToString());
var years = new List<string>();
if (AllowNull)
years.Add(" ");
for (var i = 2000; i <= 2020; i++)
years.Add(i.ToString());
if (Month.SelectedIndex > months.Count - 1)
Month.SelectedIndex = months.Count - 1;
Day.ItemsSource = days;
Month.ItemsSource = months;
Year.ItemsSource = years;
}
private void Day_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (initializing)
return;
var hour = SelectedDate != null ? SelectedDate.Value.Hour : 0;
var minute = SelectedDate != null ? SelectedDate.Value.Minute : 0;
var second = SelectedDate != null ? SelectedDate.Value.Second : 0;
initializing = true;
if (AllowNull && (Day.SelectedIndex == 0 || Month.SelectedIndex == 0 || Year.SelectedIndex == 0))
SelectedDate = null;
else
{
if (AllowNull)
SelectedDate = new DateTime(Year.SelectedIndex + 2000 - 1, Month.SelectedIndex, Day.SelectedIndex, hour, minute, second);
else
SelectedDate = new DateTime(Year.SelectedIndex + 2000, Month.SelectedIndex + 1, Day.SelectedIndex + 1, hour, minute, second);
}
//if (SelectedItem.HasValue)
// UpdateValues(SelectedItem.Value.Year, SelectedItem.Value.Month);
//else
// UpdateValues(0, 0);
initializing = false;
}
}
}
使用系统;
使用System.Collections.Generic;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
命名空间MyNamespace.Controls
{
公共密封部分类日期选择器
{
私有布尔初始化=真;
公共日期选择器()
{
初始化组件();
updateValue(0,0);
Day.SelectedIndex=0;
Month.SelectedIndex=0;
Year.SelectedIndex=0;
初始化=假;
}
公共静态只读DependencyProperty AllowNullProperty=
DependencyProperty.Register(“AllowFull”、typeof(bool)、typeof(DatePicker)、new PropertyMetadata(true));
公共布尔AllowNull
{
获取{return(bool)GetValue(AllowNullProperty);}
set{SetValue(AllowNullProperty,value);}
}
公共静态只读从属属性SelectedDateProperty=
DependencyProperty.Register(“SelectedDate”、typeof(DateTime?)、typeof(DatePicker)、new PropertyMetadata(null、OnSelectedItemChanged));
公共日期时间?选择日期
{
获取{return(DateTime?)GetValue(SelectedDateProperty);}
set{SetValue(SelectedDateProperty,value);}
}
公共事件路由EventHandler SelectedItemChanged;
SelectedItemChanged上的私有静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var ctrl=(日期选择器)d;
如果(ctrl.初始化)
返回;
ctrl.initializing=true;
ctrl.UpdateDate();
ctrl.initializing=false;
如果(ctrl.SelectedItemChanged!=null)
ctrl.SelectedItemChanged(ctrl,new RoutedEventTargets());
}
公共void UpdateDate()
{
if(SelectedDate.HasValue)
{
更新值(SelectedDate.Value.Year,SelectedDate.Value.Month);
if(AllowNull)
{
Day.SelectedIndex=SelectedDate.Value.Day;
Month.SelectedIndex=SelectedDate.Value.Month;
Year.SelectedIndex=SelectedDate.Value.Year-2000+1;
}
其他的
{
Day.SelectedIndex=SelectedDate.Value.Day-1;
Month.SelectedIndex=SelectedDate.Value.Month-1;
Year.SelectedIndex=SelectedDate.Value.Year-2000;
}
}
其他的
{
updateValue(0,0);
Day.SelectedIndex=0;
Month.SelectedIndex=0;
Year.SelectedIndex=0;
}
}
公共无效更新值(整数年,整数月)
{
var days=新列表();
if(AllowNull)
天。加上(“”);
对于(var i=1;i现在有一个DatePicker控件,用于microsoft提供的windwos应用商店应用。它随windows 8.1提供
在此处查看其他新控件:
虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。