質問

オープンソースのTwitterクライアントである Witty にテキストを表示したい、選択可能。現在、カスタムテキストブロックを使用して表示されます。 @usernameとリンクをハイパーリンクとして表示およびフォーマットするためにテキストブロックのインラインで作業しているため、TextBlockを使用する必要があります。頻繁に要求されるのは、テキストをコピーして貼り付けることです。そのためには、TextBlockを選択可能にする必要があります。

テキストブロックのようにスタイル設定された読み取り専用のTextBoxを使用してテキストを表示することで機能させようとしましたが、TextBoxにはインラインがないため、これは機能しません。つまり、TextBlockでできるように、TextBox内のテキストを個別にスタイル設定または書式設定することはできません。

アイデアはありますか

役に立ちましたか?

解決

<TextBox Background="Transparent"
         BorderThickness="0"
         Text="{Binding Text, Mode=OneWay}"
         IsReadOnly="True"
         TextWrapping="Wrap" />

他のヒント

ここでのすべての答えは、単に TextBox を使用するか、テキスト選択を手動で実装しようとしています。これにより、パフォーマンスが低下したり、ネイティブでない動作が発生します手動実装などではキーボードがサポートされていません)

何時間も掘り下げ、 WPFソースコード、代わりに TextBlock コントロール(または実際に他のコントロール)のネイティブWPFテキスト選択を有効にする方法を発見しました。テキスト選択に関する機能のほとんどは、 System.Windows.Documents.TextEditor システムクラスに実装されています。

コントロールのテキスト選択を有効にするには、2つのことを行う必要があります:

  1. TextEditor.RegisterCommandHandlers()を1回呼び出してクラスを登録します イベントハンドラー

  2. クラスのインスタンスごとに TextEditor のインスタンスを作成し、 System.Windows.Documents.ITextContainer の基になるインスタンスをそれに渡します

また、コントロールの Focusable プロパティを True に設定する必要があります。

これで終わりです!簡単に聞こえますが、残念ながら TextEditor クラスは内部としてマークされています。そのため、リフレクションラッパーを作成する必要がありました。

class TextEditorWrapper
{
    private static readonly Type TextEditorType = Type.GetType("System.Windows.Documents.TextEditor, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
    private static readonly PropertyInfo IsReadOnlyProp = TextEditorType.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
    private static readonly PropertyInfo TextViewProp = TextEditorType.GetProperty("TextView", BindingFlags.Instance | BindingFlags.NonPublic);
    private static readonly MethodInfo RegisterMethod = TextEditorType.GetMethod("RegisterCommandHandlers", 
        BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(Type), typeof(bool), typeof(bool), typeof(bool) }, null);

    private static readonly Type TextContainerType = Type.GetType("System.Windows.Documents.ITextContainer, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
    private static readonly PropertyInfo TextContainerTextViewProp = TextContainerType.GetProperty("TextView");

    private static readonly PropertyInfo TextContainerProp = typeof(TextBlock).GetProperty("TextContainer", BindingFlags.Instance | BindingFlags.NonPublic);

    public static void RegisterCommandHandlers(Type controlType, bool acceptsRichContent, bool readOnly, bool registerEventListeners)
    {
        RegisterMethod.Invoke(null, new object[] { controlType, acceptsRichContent, readOnly, registerEventListeners });
    }

    public static TextEditorWrapper CreateFor(TextBlock tb)
    {
        var textContainer = TextContainerProp.GetValue(tb);

        var editor = new TextEditorWrapper(textContainer, tb, false);
        IsReadOnlyProp.SetValue(editor._editor, true);
        TextViewProp.SetValue(editor._editor, TextContainerTextViewProp.GetValue(textContainer));

        return editor;
    }

    private readonly object _editor;

    public TextEditorWrapper(object textContainer, FrameworkElement uiScope, bool isUndoEnabled)
    {
        _editor = Activator.CreateInstance(TextEditorType, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance, 
            null, new[] { textContainer, uiScope, isUndoEnabled }, null);
    }
}

また、上記の手順を実行する TextBlock から派生した SelectableTextBlock を作成しました。

public class SelectableTextBlock : TextBlock
{
    static SelectableTextBlock()
    {
        FocusableProperty.OverrideMetadata(typeof(SelectableTextBlock), new FrameworkPropertyMetadata(true));
        TextEditorWrapper.RegisterCommandHandlers(typeof(SelectableTextBlock), true, true, true);

        // remove the focus rectangle around the control
        FocusVisualStyleProperty.OverrideMetadata(typeof(SelectableTextBlock), new FrameworkPropertyMetadata((object)null));
    }

    private readonly TextEditorWrapper _editor;

    public SelectableTextBlock()
    {
        _editor = TextEditorWrapper.CreateFor(this);
    }
}

別のオプションは、 TextBlock の添付プロパティを作成して、オンデマンドでテキストを選択できるようにすることです。この場合、選択を再度無効にするには、次のコードに相当するリフレクションを使用して、 TextEditor をデタッチする必要があります。

_editor.TextContainer.TextView = null;
_editor.OnDetach();
_editor = null;

質問に実際に答える例が見つかりませんでした。すべての答えは、TextboxまたはRichTextboxを使用しました。 TextBlockを使用できるソリューションが必要でした。これが私が作成したソリューションです。

これを行う正しい方法は、TextBlockクラスを拡張することだと思います。これは、TextBlockクラスを拡張してテキストを選択し、クリップボードにコピーできるようにするために使用したコードです。 &quot; sdo&quot; WPFで使用した名前空間参照です。

拡張クラスを使用したWPF:

xmlns:sdo="clr-namespace:iFaceCaseMain"

<sdo:TextBlockMoo x:Name="txtResults" Background="Black" Margin="5,5,5,5" 
      Foreground="GreenYellow" FontSize="14" FontFamily="Courier New"></TextBlockMoo>

拡張クラスのコードビハインド:

public partial class TextBlockMoo : TextBlock 
{
    TextPointer StartSelectPosition;
    TextPointer EndSelectPosition;
    public String SelectedText = "";

    public delegate void TextSelectedHandler(string SelectedText);
    public event TextSelectedHandler TextSelected;

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        Point mouseDownPoint = e.GetPosition(this);
        StartSelectPosition = this.GetPositionFromPoint(mouseDownPoint, true);            
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        Point mouseUpPoint = e.GetPosition(this);
        EndSelectPosition = this.GetPositionFromPoint(mouseUpPoint, true);

        TextRange otr = new TextRange(this.ContentStart, this.ContentEnd);
        otr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.GreenYellow));

        TextRange ntr = new TextRange(StartSelectPosition, EndSelectPosition);
        ntr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));

        SelectedText = ntr.Text;
        if (!(TextSelected == null))
        {
            TextSelected(SelectedText);
        }
    }
}

サンプルウィンドウコード:

    public ucExample(IInstanceHost host, ref String WindowTitle, String ApplicationID, String Parameters)
    {
        InitializeComponent();
        /*Used to add selected text to clipboard*/
        this.txtResults.TextSelected += txtResults_TextSelected;
    }

    void txtResults_TextSelected(string SelectedText)
    {
        Clipboard.SetText(SelectedText);
    }

このスタイルをTextBoxに適用するだけです(この記事):

<Style x:Key="SelectableTextBlockLikeStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="IsReadOnly" Value="True"/>
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Padding" Value="-2,0,0,0"/>
    <!-- The Padding -2,0,0,0 is required because the TextBox
        seems to have an inherent "Padding" of about 2 pixels.
        Without the Padding property,
        the text seems to be 2 pixels to the left
        compared to a TextBlock
    -->
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="False" />
                <Condition Property="IsFocused" Value="False" />
            </MultiTrigger.Conditions>
            <Setter Property="Template">
                <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <TextBlock Text="{TemplateBinding Text}" 
                             FontSize="{TemplateBinding FontSize}"
                             FontStyle="{TemplateBinding FontStyle}"
                             FontFamily="{TemplateBinding FontFamily}"
                             FontWeight="{TemplateBinding FontWeight}"
                             TextWrapping="{TemplateBinding TextWrapping}"
                             Foreground="{DynamicResource NormalText}"
                             Padding="0,0,0,0"
                                       />
                </ControlTemplate>
                </Setter.Value>
            </Setter>
        </MultiTrigger>
    </Style.Triggers>
</Style>

TextBlockのControlTemplateを作成し、読み取り専用プロパティが設定されたTextBoxを内部に配置します。 または、TextBoxを使用して読み取り専用にするだけで、TextBox.Styleを変更してTextBlockのように表示できます。

TextBlockを選択可能にするかどうかはわかりませんが、別のオプションはRichTextBoxを使用することです。これは、提案したTextBoxに似ていますが、必要な書式設定をサポートしています。

Windows開発センター

  

TextBlock.IsTextSelectionEnabledプロパティ

     

[Windows 10のUWPアプリ用に更新。Windows8.xの記事については、   アーカイブ]

     

テキスト選択が有効かどうかを示す値を取得または設定します    TextBlock で、ユーザーアクションを通じてまたは呼び出し   選択関連のAPI。

TextBlockにはテンプレートがありません。そのため、これを実現するには、textBlockとして動作するようにスタイルが変更されたTextBoxを使用する必要があります。

<Style x:Key="TextBlockUsingTextBoxStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <TextBox BorderThickness="{TemplateBinding BorderThickness}" IsReadOnly="True" Text="{TemplateBinding Text}" Background="{x:Null}" BorderBrush="{x:Null}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

質問は「選択可能」と言っていますが、意図的な結果はテキストをクリップボードに入れることだと思います。これは、クリップボードにTextblock Textプロパティ値を配置するコピーと呼ばれるコンテキストメニューとメニュー項目を追加することにより、簡単かつエレガントに実現できます。とにかくアイデアです。

このブログの投稿-トリガーを使用して、コントロールの上にカーソルを置いたときにコントロールテンプレートをスワップアウトします-パフォーマンスに役立つはずです


new TextBox
{
   Text = text,
   TextAlignment = TextAlignment.Center,
   TextWrapping = TextWrapping.Wrap,
   IsReadOnly = true,
   Background = Brushes.Transparent,
   BorderThickness = new Thickness()
         {
             Top = 0,
             Bottom = 0,
             Left = 0,
             Right = 0
         }
};

オープンソースコントロールライブラリに SelectableTextBlock を実装しました。次のように使用できます:

<jc:SelectableTextBlock Text="Some text" />
public MainPage()
{
    this.InitializeComponent();
    ...
    ...
    ...
    //Make Start result text copiable
    TextBlockStatusStart.IsTextSelectionEnabled = true;
}

@torvinの回答に追加し、 TextTrimming =&quot; CharacterEllipsis&quot; を有効にしている場合に@Dave Huangがコメントで言及したように、省略記号の上にカーソルを合わせるとアプリケーションがクラッシュします。

TextBoxの使用についてスレッドで言及されている他のオプションを試しましたが、「省略」が表示されず、テキストがコンテナ選択に合わせて長すぎる場合も、実際には解決策ではないようですテキストボックスのコンテンツは、TextBlockの動作ではない内部で「スクロール」します。

最良の解決策は@torvinの答えだと思いますが、省略記号の上にカーソルを置くとひどくクラッシュします。

それはきれいではないことはわかっていますが、未処理の例外を内部でサブスクライブ/サブスクライブ解除し、例外を処理することがこの問題を解決する唯一の方法でした。誰かがより良い解決策を持っている場合は共有してください:)

public class SelectableTextBlock : TextBlock
{
    static SelectableTextBlock()
    {
        FocusableProperty.OverrideMetadata(typeof(SelectableTextBlock), new FrameworkPropertyMetadata(true));
        TextEditorWrapper.RegisterCommandHandlers(typeof(SelectableTextBlock), true, true, true);

        // remove the focus rectangle around the control
        FocusVisualStyleProperty.OverrideMetadata(typeof(SelectableTextBlock), new FrameworkPropertyMetadata((object)null));
    }

    private readonly TextEditorWrapper _editor;

    public SelectableTextBlock()
    {
        _editor = TextEditorWrapper.CreateFor(this);

        this.Loaded += (sender, args) => {
            this.Dispatcher.UnhandledException -= Dispatcher_UnhandledException;
            this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
        };
        this.Unloaded += (sender, args) => {
            this.Dispatcher.UnhandledException -= Dispatcher_UnhandledException;
        };
    }

    private void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        if (!string.IsNullOrEmpty(e?.Exception?.StackTrace))
        {
            if (e.Exception.StackTrace.Contains("System.Windows.Controls.TextBlock.GetTextPositionFromDistance"))
            {
                e.Handled = true;
            }
        }
    }
}
Really nice and easy solution, exactly what I wanted !

いくつかの小さな修正を加えます

public class TextBlockMoo : TextBlock 
{
    public String SelectedText = "";

    public delegate void TextSelectedHandler(string SelectedText);
    public event TextSelectedHandler OnTextSelected;
    protected void RaiseEvent()
    {
        if (OnTextSelected != null){OnTextSelected(SelectedText);}
    }

    TextPointer StartSelectPosition;
    TextPointer EndSelectPosition;
    Brush _saveForeGroundBrush;
    Brush _saveBackGroundBrush;

    TextRange _ntr = null;

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);

        if (_ntr!=null) {
            _ntr.ApplyPropertyValue(TextElement.ForegroundProperty, _saveForeGroundBrush);
            _ntr.ApplyPropertyValue(TextElement.BackgroundProperty, _saveBackGroundBrush);
        }

        Point mouseDownPoint = e.GetPosition(this);
        StartSelectPosition = this.GetPositionFromPoint(mouseDownPoint, true);            
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        Point mouseUpPoint = e.GetPosition(this);
        EndSelectPosition = this.GetPositionFromPoint(mouseUpPoint, true);

        _ntr = new TextRange(StartSelectPosition, EndSelectPosition);

        // keep saved
        _saveForeGroundBrush = (Brush)_ntr.GetPropertyValue(TextElement.ForegroundProperty);
        _saveBackGroundBrush = (Brush)_ntr.GetPropertyValue(TextElement.BackgroundProperty);
        // change style
        _ntr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
        _ntr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.DarkBlue));

        SelectedText = _ntr.Text;
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top