2017-07-12 14 views
1

私はテキストブロックにいくつかのコンテンツを持っています。私はステートメントを埋めるためにラベルからテキストをドラッグアンドドロップする必要があります。それはドラッグアンドドロップを使用していくつかの文字列の空白を埋めるのが好きです。 "、" , "という個別のラベルがあります。" "、" "、" "、" in "という単語をドラッグし、空白の部分にドロップすると、「UWPはMicrosoftが作成し、Windows 10で初めて導入されたプラットフォーム同種のアプリケーションアーキテクチャ」という文を完成させる必要があります。 C#を使用してUWPで達成しようとしています。私はこれを行うのに役立ちます。 Click here to view the sampleラベルテキストをUWPのTextBlockにドラッグ&ドロップ

答えて

1

私はDragAndDropSampleManaged UWP公式コードサンプルに基づいて要件を満たしています。あなたの要求に応じて、 "a"、 "by"、 "and"、 "in"などのテキストを既存の文章にdrag and dropで挿入したいとします。だからあなたは挿入したい範囲を取得する必要があります。ポイントが解放されると、テキストが特定の範囲に挿入されます

private void TargetTextBox_DragOver(object sender, DragEventArgs e) 
{ 
    var point = e.GetPosition(TargetTextBox); 
    var range = TargetTextBox.Document.GetRangeFromPoint(point, Windows.UI.Text.PointOptions.ClientCoordinates); 
    TargetTextBox.Document.Selection.SetRange(range.StartPosition - 1, range.EndPosition);  
    TargetTextBox.Focus(FocusState.Keyboard); 
} 

enter image description here :あなたのポインタは、以下の通り推移範囲を取得するためにGetRangeFromPoint(RichEditBox)メソッドを使用することができます。

private async void TargetTextBox_Drop(object sender, Windows.UI.Xaml.DragEventArgs e) 
{ 
    VisualStateManager.GoToState(this, "Outside", true); 
    bool hasText = e.DataView.Contains(StandardDataFormats.Text); 
    // if the result of the drop is not too important (and a text copy should have no impact on source) 
    // we don't need to take the deferral and this will complete the operation faster 
    e.AcceptedOperation = hasText ? DataPackageOperation.Copy : DataPackageOperation.None; 
    if (hasText) 
    { 
     var text = await e.DataView.GetTextAsync();   
     TargetTextBox.Document.Selection.Text = text; 
    } 
} 

enter image description here

+0

グレート。ありがとう、ニコ朱。私はまた、ドラッグするコンテンツによっていくつかの文字を置き換える必要があります。例:私は入力(ドラッグ)文字列で '__'を置き換える必要があります。問題のサンプル画像を調べてください。文字列' __'の位置を見つける必要があり、同じ位置の入力で置き換えることができます(注:私はターゲットコンテンツに複数の置換可能な文字列( '__')があります。出来ますか? – Sabi

+0

私の答えを編集しました。確認してください! –

+0

はい、「_」などの特定の文字を含む選択範囲を変更できます。 –

関連する問題