我正在寻找一个使用组合框而不是日历风格的日期选择器,就像这个http://vwcmp.codeplex.com/一样,我现在正在使用这个控件,但我真的想知道是否还有别的东西。
谢谢你们,对不起我的英语。答案 0 :(得分:1)
You should make your Custom Control like this...
<UserControl x:Class="WpfApplication2.ucCalender"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="301">
<Grid Height="48" Width="303">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*" />
<ColumnDefinition Width="100*" />
<ColumnDefinition Width="100*" />
</Grid.ColumnDefinitions>
<ComboBox Height="30" HorizontalAlignment="Left" Margin="2,2,2,2" Name="cmbDays" VerticalAlignment="Top" Width="95" />
<ComboBox Grid.Column="1" Height="30" HorizontalAlignment="Left" Margin="2,2,2,2" Name="cmdMonths" VerticalAlignment="Top" Width="95" SelectionChanged="cmdMonths_SelectionChanged" />
<ComboBox Grid.Column="2" Height="30" HorizontalAlignment="Left" Margin="02,2,2,2" Name="cmbYear" VerticalAlignment="Top" Width="95" SelectionChanged="cmbYear_SelectionChanged" />
</Grid>
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for ucCalender.xaml
/// </summary>
public partial class ucCalender : UserControl
{
int[] Days = new int[31];
string[] monthNames;
List<int> years = new List<int>();
public ucCalender()
{
InitializeComponent();
initializeLists();
LoadMonthsCombos();
LoadYearCombo();
initalizeDaysArray();
LoadDaysCombo();
}
public void initializeLists()
{
monthNames = DateTimeFormatInfo.CurrentInfo.MonthNames;
years = Enumerable.Range(DateTime.Now.Year - 60, 100).ToList();
}
public void initalizeDaysArray()
{
int month = DateTimeFormatInfo.CurrentInfo.MonthNames.ToList().IndexOf(cmdMonths.SelectedValue.ToString()) + 1;
Days = new int[DateTime.DaysInMonth(Convert.ToInt32(cmbYear.SelectedValue),month)];
for (int i = 0; i < Days.Count(); i++)
{
Days[i] = i+1;
}
}
public void LoadDaysCombo()
{
cmbDays.ItemsSource = Days;
cmbDays.SelectedValue = DateTime.Now.Day;
}
public void LoadYearCombo()
{
cmbYear.ItemsSource = years;
cmbYear.SelectedValue = DateTime.Now.Year;
}
public void LoadMonthsCombos()
{
cmdMonths.ItemsSource = monthNames;
cmdMonths.SelectedValue = DateTimeFormatInfo.CurrentInfo.GetMonthName(DateTime.Now.Month);
}
private void cmdMonths_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cmdMonths.SelectedValue != null)
{
initalizeDaysArray();
LoadDaysCombo();
}
}
private void cmbYear_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cmbYear.SelectedValue != null)
{
initalizeDaysArray();
LoadDaysCombo();
}
}
}
}
USE IT LIKE THIS IN ANY WINDOW
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myCalender="clr-namespace:WpfApplication2"
Title="MainWindow" Height="295" Width="431">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="6*" />
<RowDefinition Height="46*" />
<RowDefinition Height="106*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="52*" />
<ColumnDefinition Width="301*" />
</Grid.ColumnDefinitions>
<myCalender:ucCalender Grid.Row="1" Grid.Column="2" Width="300" Height="50"/>
</Grid>
答案 1 :(得分:0)
在LoadMonthsCombo()
cmbMonths.ItemsSource = monthNames.Take(12).ToList();
我不得不使用它,因为它在12月之后的下一个空白中产生了13个选择,并导致程序崩溃。 同样在initializeLists()
monthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;