【问题标题】:how to apply different font styles in one string如何在一个字符串中应用不同的字体样式
【发布时间】:2017-11-14 18:29:05
【问题描述】:

我有一个 wpf 应用程序 c#,它在组合框关闭时显示文本(教师的姓名)。基本上它包含在 1 个字符串中并被传递给 classInstance.TeachersComboBoxTextText="{Binding Path=TeachersComboBoxText}" 。一切都很好,但如果教师被标记为非默认教师或二级教师,客户要求文本为斜体。基本上,我需要一个字符串,但应用了不同的字体样式,这取决于我的数据。

<ComboBox Name="instanceTeachersComboBox"
        IsEditable="True"
        IsReadOnly="True"
        ItemsSource="{Binding Path=TeacherList}" DropDownClosed="teachersComboBox_DropDownClosed"
        Text="{Binding Path=TeachersComboBoxText}"
        FontWeight="{Binding Path=IsSelectedFontWeight}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Path=IsSelected}" 
                        Background="{Binding Path=IsActiveColour}"
                        Content="{Binding Path=Display}" 
                        Foreground="{Binding Path=IsClashColour}" 
                        FontStyle="{Binding Path=EndDateFontStyle}"/>
                <Image Source="/SchoolEdgeTimetable;component/Images/greentick16x16.png" 
                       Margin="5,0,0,0" 
                       Visibility="{Binding Path=IsSpecialitySubjectVisibility}"></Image>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

=== 这是代码====

if (classInstance.TeacherList.Any(c => c.IsSelected == true && c.IsDefault == false) || instanceCount != defaultCount)
{
    System.Windows.Forms.RichTextBox rtf = new System.Windows.Forms.RichTextBox();
    foreach (var item in data)
    {
        if (!item.IsDefault)
        {
            rtf.SelectionFont = new Font("Arial", 12, System.Drawing.FontStyle.Italic);
            rtf.AppendText(item.Display);
        }          
        text = GT.Join2Strings(text, item.IsDefault? item.Display : rtf.Text, "\n");
    }
}
else
    text = DEFAULT_TEXT;
classInstance.TeachersComboBoxText = text;

我尝试了 rtf,因为我似乎无法在纯字符串中应用它,但代码不起作用。有没有办法在不使用 rtf 的情况下解决这个问题?或者如果不是,为什么我的代码不起作用?任何人都可以提出更好的方法来做到这一点,我们将不胜感激。谢谢

更新: 人们建议修改我已经做过的模板,这在打开组合框时显示项目非常有用。我的项目之一是红色和斜体,如您所见,但我想做的是修改组合框 TEXT 属性的显示。我附上了一张图片,以便更好地理解我的意思。 谢谢

【问题讨论】:

    标签: wpf string rtf font-style


    【解决方案1】:

    添加将字体更改为斜体的数据触发器:

    <DataTrigger Binding="{Binding IsDefault}" Value="1">
        <Setter Property="FontStyle" Value="Italic"/>
    </DataTrigger>
    

    【讨论】:

    • 但是老师的名字只附加在一个字符串中。请看我的代码。所以 IsDefault 触发器将不再起作用。这是文本,而不是实例或对象。谢谢
    【解决方案2】:

    如果你知道如何在后面的代码中使用 TextBlock 控件的&lt;Run&gt; 标签,那就很简单了。见以下代码:

    //loop through all the teachers
    foreach (var teacher in vm.Teachers)
    {
        //check if teacher is default
        if (teacher.IsDefault)
            {   
                //add text to "tb" textblock, in italic style
                tb.Inlines.Add(new Run(teacher.Name) { FontStyle = FontStyles.Italic });
            }
        else
            {
                //add text to "tb" textblock
                tb.Inlines.Add(new Run(teacher.Name));
            }
    }
    

    如果您想了解更多关于内联格式的信息,here 是一篇很酷的博文。如果您需要更多帮助,请告诉我。

    【讨论】:

    • 嗨,我已经看到了一些关于内联的东西,但我不确定我可以在组合框中使用它吗?如果您可以看到我将字符串(选定的老师 - 老师的姓名)绑定到组合框的 Text 属性而不是文本块。我会添加一个具有文本锁的模板,但如何?我怎么能说模板只在组合框关闭并且文本会出现时才适用。当它打开时,它会在一个组合框中显示常用项目,如果它关闭,它将显示所有选定教师的显示文本。谢谢
    • 是的,这正是您必须做的。您必须使用模板选择器来决定在运行时显示哪个模板。这个链接将帮助你做到这一点。 stackoverflow.com/questions/4672867/…尝试此链接中提供的解决方案,让我知道您的进度。
    • 我已经更新了我的问题。如果你有时间看它,请随意。谢谢
    【解决方案3】:

    您可以在ItemTemplate 中处理CheckBoxLoaded 事件,并将其Content 属性设置为TextBlockInline 元素,例如:

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Path=IsSelected}" 
                            Background="{Binding Path=IsActiveColour}"
                            Foreground="{Binding Path=IsClashColour}" 
                            FontStyle="{Binding Path=EndDateFontStyle}"
                            Loaded="CheckBox_Loaded">
                </CheckBox>
                <Image Source="/SchoolEdgeTimetable;component/Images/greentick16x16.png" 
                           Margin="5,0,0,0" 
                           Visibility="{Binding Path=IsSpecialitySubjectVisibility}"></Image>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    

    private void CheckBox_Loaded(object sender, RoutedEventArgs e)
    {
        CheckBox checkBox = sender as CheckBox;
        dynamic dataObject = checkBox.DataContext;
        string display = dataObject.Display;
        TextBlock textBlock = new TextBlock();
        //split the display string and add Inlines to the TextBlock as suggested by @Naresh Ravlani here...
        checkBox.Content = textBlock;
    }
    

    【讨论】:

    • 我试过这个,它在组合框打开期间添加了一个内联 texblock,而不是在关闭期间,所以这不是答案。谢谢。
    • “不在关闭期间”是什么意思?这不只是处理另一个事件(例如 DropDownClosed 事件)的问题吗?创造性思维是开发人员最好的朋友……
    • combox 文本关闭时显示,打开时显示项目。我确实有一个调用下拉关闭的事件,这就是我通过加入所有选定项目的显示文本来设置文本的地方。无论如何,没关系,我只是建议在每个老师的名字中添加一个前缀,因为修改文本模板似乎是不可能的。感谢所有试图回答的人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多