System.Windows.Documents.Run

本文介绍了一种在XAML应用中实现不同字体大小显示的方法:通过组合使用TextBlock和Run元素。此技巧允许开发者在同一段文本中灵活地改变字体大小,从而达到更好的视觉效果。

希望采用不同的方案来书写文字,可以使用多个TextBlock,也可以使用一个TextBlock+多个Run

<TextBlock FontSize="12" Margin="20">
    <Run Text="Bigbang"></Run>
    <Run Text="Let's not fall in love." FontSize="15"></Run>
</TextBlock>
效果
 

转载于:https://www.cnblogs.com/Lulus/p/8158382.html

C# using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public class StatusToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is bool isEnabled) { return isEnabled ? Brushes.Red : Brushes.Green; } return Brushes.Gray; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } public class MainViewModel : INotifyPropertyChanged { private bool _isAutoTradingEnabled; public bool IsAutoTradingEnabled { get => _isAutoTradingEnabled; set { _isAutoTradingEnabled = value; OnPropertyChanged(); OnPropertyChanged(nameof(ToggleButtonText)); } } public string ToggleButtonText => IsAutoTradingEnabled ? "关闭自动下单" : "开启自动下单"; public ICommand ToggleCommand => new RelayCommand(ToggleStatus); private void ToggleStatus(object obj) { IsAutoTradingEnabled = !IsAutoTradingEnabled; } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } public class RelayCommand : ICommand { private readonly Action<object> _execute; public RelayCommand(Action<object> execute) => _execute = execute; public bool CanExecute(object parameter) => true; public void Execute(object parameter) => _execute(parameter); public event EventHandler CanExecuteChanged; } } <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="自动下单控制" Height="200" Width="300">> <Window.Resources> <local:StatusToColorConverter x:Key="ColorConverter"/> </Window.Resources> <Window.DataContext> <local:MainViewModel/> </Window.DataContext> <Grid> <Button Content="{Binding ToggleButtonText}" Background="{Binding IsAutoTradingEnabled, Converter={StaticResource ColorConverter}}" Command="{Binding ToggleCommand}" Width="150" Height="40" FontSize="14" FontWeight="Bold" Margin="10"/> </Grid> </Window>
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值