2013-08-29 114 views

回答

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> 

UcCalendar

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 

Windows

<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> 

+0

非常感謝你 – Ruby

+0

哦提到不馬拉! – YOusaFZai