2016-08-25 75 views
0

我已經設置爲維護一個wpf應用程序,其中有一個用於記錄目的的列表框。從Datatemplate觸發器的代碼獲取綁定表達式

使用列表框的項是TextMessage類型的,即,列表框經由

ObservableCollection<TextMessage> Messages; listBox.DataContext = Messages;

結合到這些短信

消息然後與一些添加像

Messages.Add(new TextMessage("Test", TypeOfMessage.Headline));

這是該類別的定義TextMessage

public enum TypeOfMessage 
{ 
    Normal, 
    Headline, 
    Focus, 
    Important, 
    Fail, 
    Success 
} 

public class TextMessage 
{ 
    public TextMessage(string content, TypeOfMessage typeOfMessage) 
    { 
     Content = content; 
     TypeOfMessage = typeOfMessage; 
     CreationTime = DateTime.Now; 
    } 

    public string Content { get; } 
    public TypeOfMessage TypeOfMessage { get; } 
    public DateTime CreationTime { get; } 
} 

的ListBox中的XAML的定義是這樣的:

<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="196" Margin="101,77,0,0" VerticalAlignment="Top" Width="256" ItemsSource="{Binding}" SelectionMode="Multiple"> 


     <ListBox.InputBindings> 
      <KeyBinding 
        Key="C" 
        Modifiers="Control" 
        Command="Copy" 
       /> 
     </ListBox.InputBindings> 
     <ListBox.CommandBindings> 
      <CommandBinding 
        Command="Copy" 
        Executed="DoPerformCopy" 
       /> 
     </ListBox.CommandBindings> 



     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock x:Name="TextToShow" Text="{Binding Content}"></TextBlock> 
       <DataTemplate.Triggers> 
        <DataTrigger Binding="{Binding TypeOfMessage}" Value="Normal"> 
         <Setter TargetName="TextToShow" Property="Foreground" Value="Black"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding TypeOfMessage}" Value="Focus"> 
         <Setter TargetName="TextToShow" Property="Foreground" Value="Black"/> 
         <Setter TargetName="TextToShow" Property="FontWeight" Value="Bold"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding TypeOfMessage}" Value="Headline"> 
         <Setter TargetName="TextToShow" Property="Foreground" Value="RoyalBlue"/> 
         <Setter TargetName="TextToShow" Property="FontWeight" Value="Bold"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding TypeOfMessage}" Value="Important"> 
         <Setter TargetName="TextToShow" Property="Foreground" Value="Red"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding TypeOfMessage}" Value="Fail"> 
         <Setter TargetName="TextToShow" Property="Foreground" Value="Red"/> 
         <Setter TargetName="TextToShow" Property="FontWeight" Value="Bold"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding TypeOfMessage}" Value="Success"> 
         <Setter TargetName="TextToShow" Property="Foreground" Value="Green"/> 
         <Setter TargetName="TextToShow" Property="FontWeight" Value="Bold"/> 
        </DataTrigger> 
       </DataTemplate.Triggers> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 


    </ListBox> 

這很好地工作(即消息顯示在根據其類型在不同的字體粗細和顏色列表框),但現在的問題:

是否有任何方式使用BindingExpression或任何其他方式從xaml定義的代碼中獲取字體格式和顏色?

的原因是,我想只在一個地方的格式(只是在XAML,因爲它是現在),但仍然能夠重複使用它時,我想複製的內容(使用後面的代碼)包括字體格式到剪貼板。

例子:

private void DoPerformCopy() 
    { 
     RichTextBox rtb = new RichTextBox(); 
     foreach (TextMessage message in (listBox as ListBox)?.SelectedItems.Cast<TextMessage>().ToList()) 
     { 
      TextPointer startPos = rtb.CaretPosition; 
      rtb.AppendText(message.Content); 
      rtb.Selection.Select(startPos, rtb.CaretPosition.DocumentEnd); 
      // 
      // Here it would be very nice to instead having multiple switch statements to get the formatting for the 
      // TypeOfMessage from the xaml file. 
      SolidColorBrush scb = new SolidColorBrush(message.TypeOfMessage == TypeOfMessage.Fail ? Colors.Red); 
      // 

      rtb.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty, scb); 
     } 
     // Now copy the whole thing to the Clipboard 
     rtb.Selection.Select(rtb.Document.ContentStart, rtb.Document.ContentEnd); 
     rtb.Copy(); 
    } 

由於我新的WPF,我真的很感激,如果有人有解決這個小費。 (我試過很難找到在這裏計算器的解決方案,但到目前爲止,我一直不成功)提前

感謝,

王認爲 馬格努斯

+0

啊...只是指出一個解決辦法是冒險下來 路徑'listBox.ItemTemplate.Triggers' – Metscore

回答

0

請與內容設置爲一個ContentPresenter你的TextMessage。將ContentTemplate設置爲listBox.ItemTemplate並應用該模板。它將創建視覺效果(在這種情況下爲TextBlock)。然後,解析TextBlock中的值。

此外,您的RichTextBox的選擇代碼是不工作很正確,所以我固定的只是填上TextRanges到它的結束,而不是試圖讓選擇權。

private void DoPerformCopy(object sender, EventArgs e) 
{ 
    RichTextBox rtb = new RichTextBox(); 
    foreach (TextMessage message in (listBox as ListBox)?.SelectedItems.Cast<TextMessage>().ToList()) 
    { 
     ContentPresenter cp = new ContentPresenter(); 
     cp.Content = message; 
     cp.ContentTemplate = listBox.ItemTemplate; 
     cp.ApplyTemplate(); 
     var tb = VisualTreeHelper.GetChild(cp, 0) as TextBlock; 
     var fg = tb.Foreground; 
     var fw = tb.FontWeight; 

     var tr = new TextRange(rtb.Document.ContentEnd, rtb.Document.ContentEnd); 
     tr.Text = message.Content; 
     tr.ApplyPropertyValue(RichTextBox.ForegroundProperty, fg); 
     tr.ApplyPropertyValue(RichTextBox.FontWeightProperty, fw); 
    } 
    // Now copy the whole thing to the Clipboard 
    rtb.Selection.Select(rtb.Document.ContentStart, rtb.Document.ContentEnd); 
    rtb.Copy(); 
} 
+0

我應該提到的是,我從HTTP RichTextBox的代碼:// stackoverflow.com/questions/5512921/wpf-richtextbox-appending-coloured-text –

相關問題