tags:

views:

43

answers:

1

I'm new to wpf. I am writing a calendar control. I defined a default template in Generic.xaml. All the dates are populates in a listbox. Since listbox is defined in the template, I am using FindName() to access the control. The problem is that I can't call the method that populates the listbox in constructor because it's accessing the template control (which I believe is not initialized at that time. Here is the code that populate


private void DisplayCurrentMonthContents()
    {
        int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, CurrentMonth);
        TextBlock CurrentMonthTextBlock = GetTextBlock();
        ListBox DateListBox = (ListBox)Template.FindName("PART_ListBox", this);

        CurrentMonthTextBlock.Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(CurrentMonth);
        DateListBox.Items.Clear();
        for (int counter = 1; counter != daysInMonth; counter++)
        {
            DateListBox.Items.Add(counter);
        }
    }


I believe that I"m approaching it the wrong way. How can I populate the control that is used in the template with default value?

A: 

Never mind. I got the solution. Don't use the constructor. Instead override the OnApplyTemplate()

Cheers.

Sheraz