Note that there are some explanatory texts on larger screens.

plurals
  1. POIncreasing or decreasing control fontsize on runtime according to Slider(WPF component) value
    text
    copied!<p>I want to increase or decrease font size of controls such as window, treeView, ribbon menu etc that are contained by main window.</p> <p>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.</p> <p>Methods are below;</p> <pre><code> 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&lt;double&gt;(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&lt;DependencyObject&gt; GetChildren(DependencyObject reference) { int childCount = VisualTreeHelper.GetChildrenCount(reference); for (int i = 0; i &lt; childCount; i++) { yield return VisualTreeHelper.GetChild(reference, i); } } private void fontSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs&lt;double&gt; e) { ChangeControlsFontSize(this, e.NewValue - e.OldValue); } </code></pre> <p>There are some problems;</p> <p>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.</p> <p>Secondly some controls contain another controls so I increase or decrease control font size twice.</p> <p>Is there any solution for these proplems or is there another way to do this ? Could you help me please ? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload