フォントを内部要素に明示的に設定していない場合は、親フォントを継承します。したがって、親要素のいずれか(たとえば、Window
またはGrid
)のフォントサイズを変更することができます。これにより、フォントサイズを明示的に指定していないすべての内部要素のフォントサイズが変更されます。
あなたのフォントが異なるサイズでなければならない場合、私の考えでは、要素のフォントサイズを親ウィンドウのフォントサイズにバインドし、値コンバータを使用してフォントサイズをスケールすることが最も良い解決策です。
このような値変換定義:
using System;
using System.Windows.Data;
namespace WPFTest
{
public class FontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
double windowFontSize = (double)value;
var scale = System.Convert.ToDouble(parameter);
return windowFontSize * scale;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
をそして、あなたのXAMLでそれを使用する:
<Window x:Class="WPFTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:WPFTest"
Title="Window1" Height="300" Width="300" FontSize="20" x:Name="window1">
<Window.Resources>
<test:FontSizeConverter x:Key="fontSizeConverter"/>
</Window.Resources>
<Grid>
<StackPanel Grid.Row="0" Grid.Column="0">
<TextBlock
FontSize="{Binding ElementName=window1, Path=FontSize, Converter={StaticResource ResourceKey=fontSizeConverter}, ConverterParameter=1.5}">
Text 1
</TextBlock>
<TextBlock FontSize="{Binding ElementName=window1, Path=FontSize, Converter={StaticResource ResourceKey=fontSizeConverter}, ConverterParameter=0.7}">
Text 2
</TextBlock>
<TextBlock >Text 3</TextBlock>
</StackPanel>
</Grid>
</Window>
ConverterParameter
は、年度として使用されていますe(バインディングのElementName
プロパティで指定された)ウィンドウに関連する要素のフォントのe。最初TextBlock
この例フォントで
は、第TextBlock
のウィンドウのフォントとフォントの150%が、ウィンドウの70%です。 3番目のTextBlock
は、ウィンドウのフォントサイズに従います。
グリッドエレメントのユーザーが異なるフォントサイズですか? –
私は彼らのサイズを指定していませんが、同じフォントサイズを使用して必ずしも – Cynede
私の答えに別のソリューションを追加しました。 –