Hello,
I want to increase or decrease font size of controls such as window, treeView, ribbon menu etc that are contained by main window.
I have a font size slider create method and I want to acces all of Control and TextBlock by using visualtree helper and increase or decrease their font size according to slider value.
Methods are below;
private StackPanel CreateFontSizeSlider()
{
StackPanel fontSizePanel = new StackPanel();
fontSizePanel.Orientation = Orientation.Horizontal;
Slider fontSizeSlider = new Slider();
fontSizeSlider.Minimum = -3;
fontSizeSlider.Maximum = 5;
fontSizeSlider.Value = 0;
fontSizeSlider.Orientation = Orientation.Horizontal;
fontSizeSlider.TickPlacement = System.Windows.Controls.Primitives.TickPlacement.TopLeft;
fontSizeSlider.IsSnapToTickEnabled = true;
fontSizeSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(fontSizeSlider_ValueChanged);
fontSizeSlider.Width = 150;
fontSizePanel.Children.Add(fontSizeSlider);
return fontSizePanel;
}
public static void ChangeControlsFontSize(DependencyObject dependencyObject, double value)
{
foreach (DependencyObject childItem in GetChildren(dependencyObject))
{
if (childItem is Control)
{
Control control = childItem as Control;
control.FontSize = control.FontSize + value;
}
else if (childItem is TextBlock)
{
((TextBlock)childItem).FontSize = ((TextBlock)childItem).FontSize + value;
}
ChangeControlsFontSize(childItem, value);
}
}
private static IEnumerable<DependencyObject> GetChildren(DependencyObject reference)
{
int childCount = VisualTreeHelper.GetChildrenCount(reference);
for (int i = 0; i < childCount; i++)
{
yield return VisualTreeHelper.GetChild(reference, i);
}
}
private void fontSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ChangeControlsFontSize(this, e.NewValue - e.OldValue);
}
There are some problems;
Firstly I can not acces all controls by walking visual tree. For example I cannot acces closed ribbon menu items. So I can not change their fonts.
Secondly some controls contain another controls so I increase or decrease control font size twice.
Is there any solution for these proplems or is there another way to do this ? Could you help me please ?