Public class BSKeyDownBehavior : Behavior (There should only be one, but just in case.)įoreach (var item in behaviors.ToArray()) add the behavior if we don't already have one Var behaviors = Interaction.GetBehaviors(d) Private static void OnIsActiveChanged(DependencyObject d, DependencyPropert圜hangedEventArgs e)
Return (bool)control.GetValue(IsActiveProperty) Ĭontrol.SetValue(IsActiveProperty, value) Public static bool GetIsActive(FrameworkElement control) Public static readonly DependencyProperty IsActiveProperty = DependencyProperty.RegisterAttached("IsActive", typeof(bool), typeof(BSKeyDownBehavior), new PropertyMetadata(false, OnIsActiveChanged))
I referenced this post on how to add the attached behaviour to an implicit style targetting the TargetTyp="TextBox"Īnd my code: public class BackspaceSupressionBehavior
If anyone has any other suggestions which could be of use, please let me know.Īs a temporary workaround I have gone with the option 2, using an Implicit Style with an attached behaviour. I've gone ahead and submitted this as a bug: Then update all Textboxes in my app to use my inherited one?Īlternatively, will there be some proper fix for this issue on the Textbox control when running on a Mac? Is there any way to apply this to all Textboxes using an Implicit Style with a TargetType of Textbox, or is the only solution to inherit from Textbox and handle the event myself. This is what I need to work around the issue. When this event handler is attached to the textbox and the cursor position is at zero, pressing the backspace will stop the browser from navigating back a page. If (e.Key = Key.Back & textbox.SelectionStart <= 0) Private void TextBox_KeyDown(object sender, KeyEventArgs e) If the textbox has data within it, and the cursor is at position zero then hitting backspace at this point will tell the browser to navigate back a page.Īs a test, I hooked into the KeyDown event on one of my textboxes and added the following:
So the backspace issue with page navigation in the browser only seems to occur when the Textbox control has input focus and its SelectionStart position is at zero.