WPF中如何为绑定List的字符串大写字符设置不同字号
WPF ComboBox 设备名大小写区分字号的实现方案
方案一:使用 IValueConverter 生成带格式的 InlineCollection
利用 TextBlock 的 Inlines 属性可接受 InlineCollection 的特性,通过转换器将设备名字符串拆分为多个 Run 对象,分别为大写、小写字符设置不同字号。
转换器代码
using System; using System.Globalization; using System.Windows; using System.Windows.Data; using System.Windows.Documents; public class StringToMixedCaseInlinesConverter : IValueConverter { // 可通过XAML绑定自定义字号 public double UpperCaseFontSize { get; set; } = 14; public double LowerCaseFontSize { get; set; } = 12; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is not string deviceName) return DependencyProperty.UnsetValue; var inlines = new InlineCollection(); foreach (char c in deviceName) { var run = new Run(c.ToString()); run.FontSize = char.IsUpper(c) ? UpperCaseFontSize : LowerCaseFontSize; // 如需额外样式(如加粗)可在此扩展 inlines.Add(run); } return inlines; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
XAML 使用示例
先在资源中声明转换器:
<Window.Resources> <local:StringToMixedCaseInlinesConverter x:Key="MixedCaseConverter" UpperCaseFontSize="14" LowerCaseFontSize="12"/> </Window.Resources>
然后在 ComboBox 的 ItemTemplate(或自定义 ControlTemplate 的内容区域)中使用:
<ComboBox ItemsSource="{Binding AudioDevices}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock.Inlines> <Binding Path="DeviceName" Converter="{StaticResource MixedCaseConverter}"/> </TextBlock.Inlines> </TextBlock> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
方案二:使用附加属性实现灵活绑定
通过自定义附加属性,直接为 TextBlock 注入大小写区分的文本样式,无需手动处理 Inlines 绑定逻辑。
附加属性代码
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; public static class TextBlockHelper { public static readonly DependencyProperty MixedCaseTextProperty = DependencyProperty.RegisterAttached( "MixedCaseText", typeof(string), typeof(TextBlockHelper), new PropertyMetadata(null, OnMixedCaseTextChanged)); public static readonly DependencyProperty UpperCaseFontSizeProperty = DependencyProperty.RegisterAttached( "UpperCaseFontSize", typeof(double), typeof(TextBlockHelper), new PropertyMetadata(14.0, OnMixedCaseTextChanged)); public static readonly DependencyProperty LowerCaseFontSizeProperty = DependencyProperty.RegisterAttached( "LowerCaseFontSize", typeof(double), typeof(TextBlockHelper), new PropertyMetadata(12.0, OnMixedCaseTextChanged)); public static string GetMixedCaseText(TextBlock obj) => (string)obj.GetValue(MixedCaseTextProperty); public static void SetMixedCaseText(TextBlock obj, string value) => obj.SetValue(MixedCaseTextProperty, value); public static double GetUpperCaseFontSize(TextBlock obj) => (double)obj.GetValue(UpperCaseFontSizeProperty); public static void SetUpperCaseFontSize(TextBlock obj, double value) => obj.SetValue(UpperCaseFontSizeProperty, value); public static double GetLowerCaseFontSize(TextBlock obj) => (double)obj.GetValue(LowerCaseFontSizeProperty); public static void SetLowerCaseFontSize(TextBlock obj, double value) => obj.SetValue(LowerCaseFontSizeProperty, value); private static void OnMixedCaseTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not TextBlock textBlock) return; textBlock.Inlines.Clear(); var text = GetMixedCaseText(textBlock); if (string.IsNullOrEmpty(text)) return; double upperSize = GetUpperCaseFontSize(textBlock); double lowerSize = GetLowerCaseFontSize(textBlock); foreach (char c in text) { var run = new Run(c.ToString()); run.FontSize = char.IsUpper(c) ? upperSize : lowerSize; textBlock.Inlines.Add(run); } } }
XAML 使用示例
<TextBlock local:TextBlockHelper.MixedCaseText="{Binding DeviceName}" local:TextBlockHelper.UpperCaseFontSize="14" local:TextBlockHelper.LowerCaseFontSize="12"/>
关于之前转换器失败的可能原因
你之前的转换器大概率是返回了拆分后的字符串集合(而非 InlineCollection),或错误绑定到了 TextBlock.Text 属性(该属性仅接受纯文本,无法识别富文本格式)。上述两种方案均直接操作 Inlines 集合,可解决该问题。
内容的提问来源于stack exchange,提问作者MERUN KUMAR MAITY
相关产品推荐
相关产品推荐

