任何人都可以让我知道如何使转换器返回不同字体大小的文本,以便绑定的文本块可以显示它?如果TextBlock无法做到这一点,我也可以使用替代元素。
这是我现在的代码,这显然不起作用
在我的XAML文件中:
<TextBlock Text="{Binding Converter={StaticResource LabelFormatConerter}}"/>
在我的XAML.cs文件中:
public class LabelFormatConerter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
TextBlock tb = new TextBlock();
Run runLargeFont = new Run();
runLargeFont.FontSize = 18;
runLargeFont.Text = "Larger Font Text";
tb.Inlines.Add(runBase);
Run runSmallFont = new Run();
runSmallFont.FontSize = 8;
runSmallFont.BaselineAlignment = BaselineAlignment.Superscript;
runSmallFont.Text = "Smaller Font Text";
tb.Inlines.Add(runSmallFont);
return tb.Text;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
答案 0 :(得分:1)
这可能与TextBlock有关,但我不知道如何。您的转换器返回Run
个对象的集合,而Text
属性需要一个字符串。
另一种方法是使用物品控制:
<ItemsControl ItemsSource="{Binding Converter={StaticResource LabelFormatConerter}}" />
并返回
tb.Inlines
来自你的转换器。 (理想情况下,您只需在转换器中创建一个集合,而不是新的TextBlock)
答案 1 :(得分:1)
转换器不适合这项工作 - 这就是ContentTemplate
的用途。只需使用ContentControl
,将数据绑定到Content
属性,然后在ContentTemplate
中显示您想要的数据:
<ContentControl Content="{Binding Person}">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock>
<Run FontSize="18" Text="{Binding FirstName}" />
<Run FontSize="8" Text="{Binding LastName}" />
</TextBlock>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
答案 2 :(得分:1)
这应该适合你:
<TextBlock FontFamily="Calibri">
<Run>Normal Text</Run>
<Run Typography.Variants="Superscript">Test</Run>
<Run Typography.Variants="Subscript">7</Run>
</TextBlock>
并非所有字体都支持super \ _下标,因此我必须明确指定它。
您的意见是什么?两个/三个单独的值,或者您需要分成正常值,上标和下标的一个值?