0

ログ用のリストボックスがあるwpfアプリケーションを維持するように設定されています。

listbox を使用して表示されるアイテムのタイプは です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; }
}

リストボックスの 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 定義のコード ビハインドからフォントの書式設定と色を取得する方法はありますか?

その理由は、書式設定を 1 か所 (現在は 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で解決策を見つけようと懸命に努力しましたが、これまでのところ成功していません)

前もって感謝します、

キングはマグナスに敬意を表します

4

1 に答える 1

0

Content を TextMessage に設定して ContentPresenter を作成します。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();
}
于 2016-08-25T14:35:46.087 に答える