问题描述
我想增加或减小主窗口中包含的控件(如窗口,treeView,功能区菜单等)的字体大小.
I want to increase or decrease font size of controls such as window, treeView, ribbon menu etc that are contained by main window.
我有一个字体大小滑块创建方法,我想使用visualtree帮助器访问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.
制作这样的滑块:
<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组件)的值在运行时增加或减少控件字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!