2016-08-25 15 views
0

ロギング用のリストボックスがあるwpfアプリケーションを維持するように設定されています。リストボックスがDatatemplateトリガーからのコードビーンディングからのバインディング式の取得

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

メッセージが、その後

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

このような何かを追加する介して、これらのテキストメッセージにバインドされている。すなわち

リストボックスを使用して表示される項目は、タイプTextMessageでありますクラスの定義です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; } 
} 

リストボックスのための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> 

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する新たなんだので、誰かがこれを解決するためのヒントを持っている場合、私は本当に感謝します。 (私はstackoverflowの時にここに解決策を見つけるために懸命に試みましたが、今のところ、私は失敗してきた)事前に

おかげで、

王はへコンテンツセットでのContentPresenterを作る マグナス

+0

ああ...ただ、一つの解決策は、私は私がHTTPからリッチテキストボックスのコードを持っていることを述べてきたはずです 'listBox.ItemTemplate.Triggers' – Metscore

答えて

0

についてあなたのTextMessage。 ListBox.ItemTemplateにContentTemplateを設定し、テンプレートを適用します。ビジュアルを作成します(この場合は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

の道をベンチャーことであろうと指摘しましたstackoverflow.com/questions/5512921/wpf-richtextbox-appending-coloured-text –