2013-04-07 126 views
4

我正在開發一個WPF項目,並且我有一個月組合框,應該包含從1月到12月的幾個月。我設法通過以下代碼片段完成它;用幾個月和數字填充組合框幾個月

var americanCulture = new CultureInfo("en-US"); 
ddlMonth.Items.AddRange(americanCulture.DateTimeFormat.MonthNames); 

並得到了名單;

January 
February 
March 
.... 

但我希望他們填充爲;

01 - January 
02 - February 
03 - March 
..... 

任何想法如何繼續for循環?我是C#的新手。由於

+0

你真的想顯示的月份數,或者你想顯示'April',並有選擇的值是'4'? – Corak 2013-04-07 09:40:19

+0

不,我想用月份名稱顯示月份號碼。 – AbdulAziz 2013-04-07 10:03:41

回答

4
 var americanCulture = new CultureInfo("en-US"); 
     var count = 1; 
     //.Take(12) in the foreach below because apparantly MonthNames returns 13 
     //elements of which the last one is empty 
     foreach (var month in americanCulture.DateTimeFormat.MonthNames.Take(12)) 
     {  
     DropDownList1.Items.Add(new ListItem{Text = count.ToString("00") + "-" + month}); 
     count++; 
     } 
+1

'count.ToString(「00」)+「 - 」+ month' – Corak 2013-04-07 09:41:07

2

試試這個:

List<string> names = new List<string>(); 
     int num = 1; 
     foreach (var item in americanCulture.DateTimeFormat.MonthNames) 
     { 
      names.Add(string.Format("{0} - {1}", num++.ToString("D2"), item)); 
     } 
     ddlMonth.Items.AddRange(names); 
2

這樣做將有一天和組合框當月的組合框的最佳方式。

Populating month and day

使用

for (int i = 1; i <= DateTime.DaysInMonth(year, month); i++) { 
    cmbDay.Items.Add(i.ToString()); 
} 
6

野應。你爲什麼在後端做這件事。利用wpf並使用xaml。

讓我試着解釋如何做到這一點。

  1. 首先爲月份創建一個依賴項屬性。如果你還沒有使用依賴屬性。你現在應該研究他們http://wpftutorial.net/DependencyProperties.html。在下面的代碼中,我通過新的PropertyMetadata(new CultureInfo(「en-US」)DateTimeFormat.MonthNames.Take(12).ToList()))將該屬性的默認值設置爲月份列表。所以現在我們可以從xaml獲得這個屬性。

    public partial class MainWindow : Window 
    { 
    public static readonly DependencyProperty MonthsProperty = DependencyProperty.Register(
        "Months", 
        typeof(List<string>), 
        typeof(MainWindow), 
        new PropertyMetadata(new CultureInfo("en-US").DateTimeFormat.MonthNames.Take(12).ToList())); 
    
    public List<string> Months 
    { 
        get 
        { 
         return (List<string>)this.GetValue(MonthsProperty); 
        } 
    
        set 
        { 
         this.SetValue(MonthsProperty, value); 
        } 
    } 
    public MainWindow() 
    { 
        InitializeComponent();    
    } 
    

    }

  2. 你需要一個轉換器。 http://wpftutorial.net/ValueConverters.html如果不熟悉它們。轉換器要做的是爲列表中的每個值我們將修改字符串以獲得所需的結果。因此,爲價值轉換器創建一個新類。

    [ValueConversion(typeof(List<string>), typeof(List<string>))] 
    public class MonthConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
        //get the list of months from the object sent in  
        List<string> months = (List<string>)value; 
    
        //manipulate the data index starts at 0 so add 1 and set to 2 decimal places 
        if (months != null && months.Count > 0) 
        { 
         for (int x = 0; x < months.Count; x++) 
         { 
          months[x] = (x + 1).ToString("D2") + " - " + months[x]; 
         } 
        } 
    
        return months; 
    } 
    
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
    
    #endregion 
    

    }

  3. 設置它在XAML。爲你的窗口創建一個名字,例如x:Name =「testWindow」,這樣可以在綁定時更好地訪問它。設置您的命名空間,以便您可以獲得轉換器。 xmlns:Main =「clr-namespace:WpfApplication4」。將轉換器添加到資源中。在組合框中,將itemssource綁定到您的依賴屬性Month並通過轉換器發送。

    <Window x:Class="WpfApplication4.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        x:Name="testwindow" 
        xmlns:Main="clr-namespace:WpfApplication4" 
        Title="MainWindow" Height="350" Width="525"> 
    
        <Window.Resources> 
         <Main:MonthConverter x:Key="MonthConverter"/> 
        </Window.Resources> 
    
        <Grid> 
         <ComboBox ItemsSource="{Binding ElementName=testwindow,Path=Months, Converter={StaticResource MonthConverter}}" HorizontalAlignment="Left" Margin="197,107,0,0" VerticalAlignment="Top" Width="120"/> 
    
        </Grid> 
    </Window> 
    
+0

您還應該在組合框 user892381 2013-04-07 18:06:50

+0

嗯抱歉,沒有看到你對c#新手的部分,所以這可能有點多..我用什麼是必須知道的wpf ..依賴屬性,綁定,值轉換器。 wpftutorial.net非常適合學習基礎知識.. – user892381 2013-04-07 18:30:42