Wpf 如何使用默认值填充模板中使用的控件?
我是wpf的新手。我正在写一个日历控件。我在Generic.xaml中定义了一个默认模板。所有日期都填充在列表框中。因为listbox是在模板中定义的,所以我使用FindName()来访问控件。问题是,我无法调用在构造函数中填充listbox的方法,因为它正在访问模板控件(我相信当时没有初始化该控件)Wpf 如何使用默认值填充模板中使用的控件?,wpf,Wpf,我是wpf的新手。我正在写一个日历控件。我在Generic.xaml中定义了一个默认模板。所有日期都填充在列表框中。因为listbox是在模板中定义的,所以我使用FindName()来访问控件。问题是,我无法调用在构造函数中填充listbox的方法,因为它正在访问模板控件(我相信当时没有初始化该控件) 我相信我的方法是错误的。如何用默认值填充模板中使用的控件?没关系。我找到了解决方案。不要使用构造函数。而是重写OnApplyTemplate() 干杯 private void Display
我相信我的方法是错误的。如何用默认值填充模板中使用的控件?没关系。我找到了解决方案。不要使用构造函数。而是重写OnApplyTemplate() 干杯
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);
}
}