-1

我有一个 wpf 应用程序 c#,它在组合框关闭时显示文本(教师的姓名)。基本上它包含在 1 个字符串中并被传递给 classInstance.TeachersComboBoxTextor Text="{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 属性的显示。我附上了一张图片,以便更好地理解我的意思。谢谢 在此处输入图像描述

4

3 回答 3

0

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

//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));
        }
}

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

于 2017-06-13T07:26:52.813 回答
0

您可以处理in 的Loaded事件并将其属性设置为 a of元素,例如:CheckBoxItemTemplateContentTextBlockInline

<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;
}
于 2017-06-13T09:34:13.010 回答
-1

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

<DataTrigger Binding="{Binding IsDefault}" Value="1">
    <Setter Property="FontStyle" Value="Italic"/>
</DataTrigger>
于 2017-06-13T06:32:28.400 回答