根据 Slider(WPF 组件)值在运行时增加或减少控制字体大小 [英] Increasing or decreasing control fontsize on runtime according to Slider(WPF component) value

查看:27
本文介绍了根据 Slider(WPF 组件)值在运行时增加或减少控制字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想增加或减少主窗口包含的窗口、树视图、功能区菜单等控件的字体大小.

I want to increase or decrease font size of controls such as window, treeView, ribbon menu etc that are contained by main window.

我有一个字体大小滑块创建方法,我想通过使用可视化树助手访问所有 Control 和 TextBlock,并根据滑块值增加或减少它们的字体大小.

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.

方法如下;

 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);
        }

有一些问题;

首先,我无法通过走可视化树来访问所有控件.例如,我无法访问关闭的功能区菜单项.所以我不能改变他们的字体.

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 ?

推荐答案

你工作太辛苦了.:-D

You are working way too hard. :-D

创建这样的样式:

<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>

在您的应用程序上创建一个名为 fontSize 的属性.

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!

这篇关于根据 Slider(WPF 组件)值在运行时增加或减少控制字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆