WPF TextBox and preserving tabs
Is there some elegant way how to allow pasting into WPF TextBox that has AcceptsTab set to false while preserving tabs in the pasted text ?
When AcceptsTab is true, then user can't use tabs to move to next control, which isn't desired by my users. But they want to have tabs that are pasted, which currently are replaced by spaces.
Thank you
wpf xaml
add a comment |
Is there some elegant way how to allow pasting into WPF TextBox that has AcceptsTab set to false while preserving tabs in the pasted text ?
When AcceptsTab is true, then user can't use tabs to move to next control, which isn't desired by my users. But they want to have tabs that are pasted, which currently are replaced by spaces.
Thank you
wpf xaml
add a comment |
Is there some elegant way how to allow pasting into WPF TextBox that has AcceptsTab set to false while preserving tabs in the pasted text ?
When AcceptsTab is true, then user can't use tabs to move to next control, which isn't desired by my users. But they want to have tabs that are pasted, which currently are replaced by spaces.
Thank you
wpf xaml
Is there some elegant way how to allow pasting into WPF TextBox that has AcceptsTab set to false while preserving tabs in the pasted text ?
When AcceptsTab is true, then user can't use tabs to move to next control, which isn't desired by my users. But they want to have tabs that are pasted, which currently are replaced by spaces.
Thank you
wpf xaml
wpf xaml
edited Nov 23 '18 at 20:33
Erno de Weerd
44.8k96795
44.8k96795
asked Nov 23 '18 at 20:16
B JB J
1871212
1871212
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).
See Paste Event in a WPF TextBox
Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:
XAML:
<TextBox AcceptsTab="False"
Height="200"
PreviewKeyDown="TextBox_PreviewKeyDown"
KeyUp="TextBox_KeyUp"/>
C#:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
{
textbox.AcceptsTab = true;
}
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
textbox.AcceptsTab = false;
}
This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.
add a comment |
Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.
The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
var textBox = sender as TextBox;
if (textBox != null)
{
var direction =
e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
? FocusNavigationDirection.Previous
: FocusNavigationDirection.Next;
textBox.MoveFocus(new TraversalRequest(direction));
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452506%2fwpf-textbox-and-preserving-tabs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).
See Paste Event in a WPF TextBox
Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:
XAML:
<TextBox AcceptsTab="False"
Height="200"
PreviewKeyDown="TextBox_PreviewKeyDown"
KeyUp="TextBox_KeyUp"/>
C#:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
{
textbox.AcceptsTab = true;
}
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
textbox.AcceptsTab = false;
}
This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.
add a comment |
I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).
See Paste Event in a WPF TextBox
Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:
XAML:
<TextBox AcceptsTab="False"
Height="200"
PreviewKeyDown="TextBox_PreviewKeyDown"
KeyUp="TextBox_KeyUp"/>
C#:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
{
textbox.AcceptsTab = true;
}
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
textbox.AcceptsTab = false;
}
This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.
add a comment |
I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).
See Paste Event in a WPF TextBox
Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:
XAML:
<TextBox AcceptsTab="False"
Height="200"
PreviewKeyDown="TextBox_PreviewKeyDown"
KeyUp="TextBox_KeyUp"/>
C#:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
{
textbox.AcceptsTab = true;
}
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
textbox.AcceptsTab = false;
}
This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.
I am not sure this qualifies as elegant but it works but might not be as complete as you want (e.g. when right-clicking in the textbox and selecting Paste from the context menu).
See Paste Event in a WPF TextBox
Set the AcceptsTab to true just before Ctrl-V is processed and restore it after:
XAML:
<TextBox AcceptsTab="False"
Height="200"
PreviewKeyDown="TextBox_PreviewKeyDown"
KeyUp="TextBox_KeyUp"/>
C#:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
if (e.Key == Key.V && (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)))
{
textbox.AcceptsTab = true;
}
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (!(sender is TextBox textbox))
{
return;
}
textbox.AcceptsTab = false;
}
This could be turned into a behavior so it would be easier to apply it to more textboxes without writing code behind.
edited Nov 23 '18 at 20:50
answered Nov 23 '18 at 20:33
Erno de WeerdErno de Weerd
44.8k96795
44.8k96795
add a comment |
add a comment |
Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.
The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
var textBox = sender as TextBox;
if (textBox != null)
{
var direction =
e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
? FocusNavigationDirection.Previous
: FocusNavigationDirection.Next;
textBox.MoveFocus(new TraversalRequest(direction));
}
}
}
add a comment |
Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.
The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
var textBox = sender as TextBox;
if (textBox != null)
{
var direction =
e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
? FocusNavigationDirection.Previous
: FocusNavigationDirection.Next;
textBox.MoveFocus(new TraversalRequest(direction));
}
}
}
add a comment |
Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.
The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
var textBox = sender as TextBox;
if (textBox != null)
{
var direction =
e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
? FocusNavigationDirection.Previous
: FocusNavigationDirection.Next;
textBox.MoveFocus(new TraversalRequest(direction));
}
}
}
Another approach is by setting AcceptsTab to true and moving the focus when (Shift) Tab is pressed.
The nice side effect is that all the copy/paste scenarios will still function but the user will not be able to type a tab.
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
var textBox = sender as TextBox;
if (textBox != null)
{
var direction =
e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)
? FocusNavigationDirection.Previous
: FocusNavigationDirection.Next;
textBox.MoveFocus(new TraversalRequest(direction));
}
}
}
answered Nov 29 '18 at 8:22
Erno de WeerdErno de Weerd
44.8k96795
44.8k96795
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452506%2fwpf-textbox-and-preserving-tabs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown