Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何使用默认值填充模板中使用的控件?_Wpf - Fatal编程技术网

Wpf 如何使用默认值填充模板中使用的控件?

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

我是wpf的新手。我正在写一个日历控件。我在Generic.xaml中定义了一个默认模板。所有日期都填充在列表框中。因为listbox是在模板中定义的,所以我使用FindName()来访问控件。问题是,我无法调用在构造函数中填充listbox的方法,因为它正在访问模板控件(我相信当时没有初始化该控件)




我相信我的方法是错误的。如何用默认值填充模板中使用的控件?

没关系。我找到了解决方案。不要使用构造函数。而是重写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);
        }
    }