英文:
How to change the size of some elements of WPF DatePicker
问题
我正在编写一个C# WPF应用程序。
在一个窗口中,我放置了两个DatePicker对象:我能够放大日历的字体并放大图像图标。
我还需要放大日历的前两行(月份+年份)以及星期几的行(请参见图像..)的字体。
我创建了一个DatePicker控件的样式副本,但我无法识别要更改的元素。
我为CalendarDayButton、CalendarDayButton、CalendarItem定义了一个新的Setter,将字体设置为36px,但我看不到结果。
请问如何实现这一点?
英文:
I'm writing a C# WPF application.
In a window i put two DatePicker objects: I was able to enlarge the font of the calendar and enlarge the image icon.
I also need to enlarge the font of the two first lines of the calendar (month + year) and the row of the days of week (see the image ..).
I created a copy of the style for DatePicker control but I do not identify the element to change in it.
I defined a new setter for CalendarDayButton, CalendarDayButton, CalendarItem to set font to 36px but I dont see the result.
How can achieve that, please?
答案1
得分: 1
让我用 VisualTreeHelper
来展示一种方法。
首先,您需要通过 VisualTree 来搜索控件。
public static IEnumerable<T> FindChildren<T>(DependencyObject parent)
where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T childAsT)
{
yield return childAsT;
}
foreach (T grandChild in FindChildren<T>(child))
{
yield return grandChild;
}
}
}
然后,在 DatePicker
的 CalendarOpened
事件处理程序中,我们可以找到 Popup
和其中的控件。Popup.Child
是一个 Calender
控件,其中包含 50 个 TextBlock
。索引为 1 到 7 的项目是星期几的 TextBlock
。
private void DatePickerControl_CalendarOpened(object sender, RoutedEventArgs e)
{
if (FindChildren<Popup>(DatePickerControl).FirstOrDefault() is not Popup popup)
{
return;
}
double fontSize = 20;
if (FindChildren<Button>(popup.Child)
.Where(x => x.Name == "PART_HeaderButton")
.FirstOrDefault() is Button headerButton)
{
headerButton.FontSize = fontSize;
}
if (FindChildren<TextBlock>(popup.Child) is IEnumerable<TextBlock> textBlocks)
{
int targetChildIndex = 1;
for (int i = 0; i < 7; i++)
{
TextBlock dayOfTheWeedTextBlock = textBlocks.ElementAt(targetChildIndex);
dayOfTheWeedTextBlock.FontSize = fontSize;
var text = dayOfTheWeedTextBlock.Text;
targetChildIndex++;
}
}
if (FindChildren<Grid>(popup.Child)
.Where(x => x.Name == "PART_YearView")
.FirstOrDefault() is Grid yearViewGrid)
{
foreach (CalendarButton calendarButton in FindChildren<CalendarButton>(yearViewGrid))
{
calendarButton.FontSize = fontSize;
}
}
if (FindChildren<Button>(popup.Child)
.Where(x => x.Name == "PART_NextButton")
.FirstOrDefault() is Button nextButton)
{
nextButton.RenderTransform = new ScaleTransform(1.5, 1.5);
nextButton.RenderTransformOrigin = new Point(0.5, 0.5);
}
}
英文:
Let me show you a way using the VisualTreeHelper
.
First, you need to have a way to search controls through the VisualTree.
public static IEnumerable<T> FindChildren<T>(DependencyObject parent)
where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T childAsT)
{
yield return childAsT;
}
foreach (T grandChild in FindChildren<T>(child))
{
yield return grandChild;
}
}
}
Then, in the DatePicker
's CalendarOpened
event handler, we can find the Popup
and controls inside it. The Popup.Child
is a Calender
control, which has 50 TextBlock
s inside it. The items with index 1 to 7 are the TextBlocks
for the days of the week.
private void DatePickerControl_CalendarOpened(object sender, RoutedEventArgs e)
{
if (FindChildren<Popup>(DatePickerControl).FirstOrDefault() is not Popup popup)
{
return;
}
double fontSize = 20;
if (FindChildren<Button>(popup.Child)
.Where(x => x.Name == "PART_HeaderButton")
.FirstOrDefault() is Button headerButton)
{
headerButton.FontSize = fontSize;
}
if (FindChildren<TextBlock>(popup.Child) is IEnumerable<TextBlock> textBlocks)
{
int targetChildIndex = 1;
for (int i = 0; i < 7; i++)
{
TextBlock dayOfTheWeedTextBlock = textBlocks.ElementAt(targetChildIndex);
dayOfTheWeedTextBlock.FontSize = fontSize;
var text = dayOfTheWeedTextBlock.Text;
targetChildIndex++;
}
}
if (FindChildren<Grid>(popup.Child)
.Where(x => x.Name == "PART_YearView")
.FirstOrDefault() is Grid yearViewGrid)
{
foreach (CalendarButton calendarButton in FindChildren<CalendarButton>(yearViewGrid))
{
calendarButton.FontSize = fontSize;
}
}
if (FindChildren<Button>(popup.Child)
.Where(x => x.Name == "PART_NextButton")
.FirstOrDefault() is Button nextButton)
{
nextButton.RenderTransform = new ScaleTransform(1.5, 1.5);
nextButton.RenderTransformOrigin = new Point(0.5, 0.5);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论