2

我正在寻找一个使用组合框而不是日历样式的日期选择器,就像这个http://vwcmp.codeplex.com/一样,我现在正在使用这个控件,但我真的很想知道是否还有其他东西。

谢谢大家,对不起我的英语。

4

2 回答 2

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>

于 2013-08-29T06:43:19.850 回答
0

在 LoadMonthsCombo()

cmbMonths.ItemsSource = monthNames.Take(12).ToList();

我不得不使用,因为它在 12 月之后的下一个生成 13 个选项是空白的并且使程序崩溃。同样在 initializeLists()

 monthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
于 2019-03-03T15:21:58.867 回答