2017-06-13 93 views
-1

我有一個wpf應用程序,c#在組合框關閉時顯示文本(teahcer的名稱)。基本上它包含在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屬性。我附上了一張圖片,以便更好地瞭解我的意思。 感謝 enter image description here

回答

-1

添加,改變您的字體以斜體一個datatrigger:

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

但老師的名字被附加在一起只有一個字符串。請參閱我的代碼。所以IsDefault觸發器將不再起作用。這是一個文本,而不是一個實例或對象。謝謝 – user742102

0

,如果你知道如何利用TextBlock控件的<Run>標籤在後面的代碼這很簡單。請參見下面的代碼:

//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是一個很酷的博客文章一樣。讓我知道你是否需要更多的幫助。

+0

嗨,我看到一些關於內聯已經但我不知道我可以在組合框內使用它?如果您能看到我將字符串(選定的教師 - 教師姓名)綁定到組合框的Text屬性而不是文本塊。我會添加一個有文本鎖的模板,但是如何?我怎麼能說,該模板只適用於組合框關閉和文本將出現。打開時,它會顯示組合框中的常用項目,如果它關閉,它將顯示所有選定教師的顯示文本。謝謝 – user742102

+0

是的,這正是你必須要做的。您必須使用模板選擇器來決定在運行時顯示哪個模板。這個鏈接將幫助你做到這一點。 https://stackoverflow.com/questions/4672867/can-i-use-a-different-template-for-the-selected-item-in-a-wpf-combobox-than-for 嘗試此鏈接提供的解決方案並讓我知道你的進步。 –

+0

我已更新我的問題。如果你有時間看它,請隨意。謝謝 – user742102

0

你可以處理Loaded事件的CheckBoxItemTemplate及其Content屬性設置爲InlineTextBlock元素,如:

<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; 
} 
+0

我試過了,它在combobox打開時添加了一個內聯texblock,而不是在關閉期間,所以這不是答案。謝謝。 – user742102

+0

你是什麼意思「不關閉」?這不僅僅是處理另一個事件,比如DropDownClosed事件嗎?創造性思維是開發者最好的朋友...... – mm8

+0

當它被關閉時顯示combobx文本,而當它被打開時,項目被顯示。我確實有一個關閉下拉菜單的事件,這是我通過連接所有選定項目的顯示文本來設置文本的位置。無論如何,沒關係,我只是建議在每個老師的名字中加上一個前綴,因爲似乎不可能修改文本模板。感謝所有試圖回答的人。 – user742102

相關問題