0
여러 개의 실행이있는 TextBlock이있는 경우 어떻게 상대 글꼴 크기를 지정할 수 있습니까? 예를 들어 TextBlock에 FontSize 10이있는 경우 Runs가 15가되도록합니다. FontSize가 20이되면 Run이 30이되도록하십시오.실행시 상대 글꼴 크기 지정
여러 개의 실행이있는 TextBlock이있는 경우 어떻게 상대 글꼴 크기를 지정할 수 있습니까? 예를 들어 TextBlock에 FontSize 10이있는 경우 Runs가 15가되도록합니다. FontSize가 20이되면 Run이 30이되도록하십시오.실행시 상대 글꼴 크기 지정
WPF 또는 기타 XAML 프레임 워크. 이 샘플은 WPF에서 작동합니다.
일반적으로 바인딩 및 값 변환기를 사용하여 수행됩니다.
ValueConverter 코드
class FontSizeConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture) {
// add input parameter testing as needed.
var originalFontSize = (double)value;
double alteredFontSize = originalFontSize * Ratio; ;
return alteredFontSize;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
// Create a ratio property
// allows the converter to be used for different font ratios
public double Ratio { get; set; }
}
인스턴스화하여 XAML 자원이 변환기와 비율 속성을 설정합니다.
<Window.Resources>
<local:FontSizeConverter x:Key='FontSizeConverter'
Ratio='1.5' />
</Window.Resources>
XAML은 그런 부모 TextBlock의 폰트 크기를 얻기 위해 상대 바인딩을 사용합니다. 값 부모 값을 ValueConverter에 전달하십시오. XAML
<TextBlock FontSize='20'>
<Run Text='The first bit.'
FontSize='{Binding FontSize, Converter={StaticResource FontSizeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBlock}}' />
<Run Text='The second bit' />
<Run Text='The third bit' />
</TextBlock>
당신이 각 실행에 대한 바인딩을 반복하거나 바인딩을 사용하는 스타일을 만들 수 있습니다 TextBlock의 모든 런에 바인딩을 적용 할 경우
.
XAML
<Style TargetType='Run'>
<Setter Property='FontSize'
Value='{Binding FontSize, Converter={StaticResource FontSizeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBlock}}' />
</Style>
전체 XAML
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local='clr-namespace:SO_Questions'
x:Class="SO_Questions.TextRunWindow"
Title="TextRunWindow"
Height="600"
Width="600">
<Window.Resources>
<local:FontSizeConverter x:Key='FontSizeConverter'
Ratio='2.5' />
<Style TargetType='Run'>
<Setter Property='FontSize'
Value='{Binding FontSize, Converter={StaticResource FontSizeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBlock}}' />
</Style>
</Window.Resources>
<Grid>
<TextBlock FontSize='20'>
<Run Text='The first bit.'
FontSize='{Binding FontSize, Converter={StaticResource FontSizeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBlock}}' /><Run Text=' ' />
<Run Text='The second bit' />
<Run Text='The third bit' />
</TextBlock>
</Grid>
</Window>