【问题标题】:In WPF Textblock with multiple runs how do i apply a style to a particular one?在具有多次运行的 WPF 文本块中,我如何将样式应用于特定的?
【发布时间】:2015-11-01 09:34:45
【问题描述】:

如何不将样式应用于整个文本块,而仅应用于第一次运行(粗体)?

我想在 Bold Run 上应用样式“XXXFontName-Bold”,在其余部分应用样式“XXXFontName-Thin”。

        // add button
        Button btn = new Button();
        TextBlock contextText = new TextBlock();
        contextText.Inlines.Add(new Bold(new Run(label.Substring(0,1))));
        contextText.Inlines.Add(new Style());     <===== OBVIOUS ERROR HERE
        contextText.Inlines.Add(label.Substring(1));
        contextText.FontSize = 25;
        contextText.Style = FindResource("XXXFontName-Thin") as Style;
        btn.Content = contextText;

【问题讨论】:

    标签: c# wpf styling


    【解决方案1】:

    3 示例样式,设置示例在 XAML 中运行,带有样式和新行,以及如何在按钮后面的代码中设置它们

    你的代码:

    public MainWindow()
    {
        InitializeComponent();
    
        Button btn = new Button();
        TextBlock contextText = new TextBlock();
        var newRun = new Run("BoldGreenRunStyle");
        newRun.Style = FindResource("BoldGreenRunStyle") as Style;
        contextText.Inlines.Add(newRun);
    
        newRun = new Run("ItalicRedRunStyle");
        newRun.Style = FindResource("ItalicRedRunStyle") as Style;
        contextText.Inlines.Add(newRun);
    
        newRun = new Run("ThinPurpleRunStyle");
        newRun.Style = FindResource("ThinPurpleRunStyle") as Style;
        contextText.Inlines.Add(newRun);
    
        btn.Content = contextText;
    
        Container.Children.Add(btn);
    }
    

    你的 XAML

        <Window.Resources>
            <Style TargetType="Run" x:Key="BoldGreenRunStyle">
                <Setter Property="Foreground" Value="Green"></Setter>
                <Setter Property="FontWeight" Value="Bold"></Setter>
            </Style>
            <Style TargetType="Run" x:Key="ItalicRedRunStyle">
                <Setter Property="Foreground" Value="Red"></Setter>
                <Setter Property="FontWeight" Value="Normal"></Setter>
                <Setter Property="FontStyle" Value="Italic"></Setter>
            </Style>
            <Style TargetType="Run" x:Key="ThinPurpleRunStyle">
                <Setter Property="Foreground" Value="Purple"></Setter>
                <Setter Property="FontWeight" Value="Thin"></Setter>
            </Style>
        </Window.Resources>
        <StackPanel x:Name="Container">
            <Label Content="From XAML"></Label>
            <TextBlock>
                <TextBlock.Inlines>
                    <Run Style="{StaticResource BoldGreenRunStyle}">BoldGreenRunStyle</Run>
                    <LineBreak/>
                    <Run Style="{StaticResource ItalicRedRunStyle}">ItalicRedRunStyle</Run>
                    <LineBreak/>
                    <Run Style="{StaticResource ThinPurpleRunStyle}">ThinPurpleRunStyle</Run>
                    <LineBreak/>
                </TextBlock.Inlines>
            </TextBlock>
        </StackPanel>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2011-01-02
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多