views:

337

answers:

1

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 ?

A: 

You are working way too hard. :-D

Create a style like this:

<Style TargetType="ListBox">
    <Setter Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize">
        <Setter.Value>
            <Binding Source="{x:Static Application.Current}" Path="fontSize"/>
        </Setter.Value>
    </Setter>
</Style>

Create a property called fontSize on your application.

Make a slider like this:

    <Slider Name="fontSize" Minimum="10" Maximum="22" IsSnapToTickEnabled="True"  TickPlacement="TopLeft"
            Value="{Binding Source={x:Static Application.Current}, Path=fontSize, Mode=TwoWay}"/>

Now, any control with that style will nicely resize - and no code is needed!

Michael