dotnet / dotnet/wpf

EditBox doesn't work well standalone

Open
#1,335 0 comments 0 reactions 0 assignees View on GitHub
Enhancement Requested
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

@nick2893 commented on [Tue Jul 16 2019](https://github.com/microsoft/WPF-Samples/issues/95)

I propose to split up EditBox.cs into two halves, first of which works without GridView View requirement as the following. Second half would inherit the first.

Sorry, new to GitHub, not sure how this works.

using System.Windows.Controls;

using System.Windows.Input;

using System.Windows;

using System.Windows.Documents;

namespace EditBoxControlLibrary

{

public class EditBoxBase : TextBlock

{

#region Static Constructor

static EditBoxBase()

{

DefaultStyleKeyProperty.OverrideMetadata(typeof(EditBoxBase), new FrameworkPropertyMetadata(typeof(EditBoxBase)));

}

#endregion

#region Public Methods

public EditBoxBase()

{

this.Loaded += EditBoxBase_Loaded;

_textBox = new TextBox();

_textBox.KeyDown += new KeyEventHandler(OnTextBoxKeyDown);

_textBox.LostKeyboardFocus += new KeyboardFocusChangedEventHandler(OnTextBoxLostKeyboardFocus);

}

bool bLoadedFinished = false;//Not sure why EditBoxBase_Loaded fires twice ... 20190711

private void EditBoxBase_Loaded(object sender, RoutedEventArgs e)

{

if (bLoadedFinished) return;

bLoadedFinished = true;

TextBlock textBlock = this as TextBlock;

this.MouseEnter += EditBoxBase_MouseEnter;

this.MouseLeave += EditBoxBase_MouseLeave;

this.MouseUp += EditBoxBase_MouseUp;

_adorner = new EditBoxBaseAdorner(textBlock, _textBox);

AdornerLayer layer = AdornerLayer.GetAdornerLayer(textBlock); // fails on getting adorner if this is in constructor

layer.Add(_adorner);

}

#endregion

#region Protected Methods

protected void EditBoxBase_MouseEnter(object sender, MouseEventArgs e)

{

if (!IsEditing)

{

_canBeEdit = true;

}

}

protected void EditBoxBase_MouseLeave(object sender, MouseEventArgs e)

{

_isMouseWithinScope = false;

_canBeEdit = false;

}

protected void EditBoxBase_MouseUp(object sender, MouseButtonEventArgs e)

{

if (e.ChangedButton == MouseButton.Right || e.ChangedButton == MouseButton.Middle)

return;

if (!IsEditing)

{

if (!e.Handled && (_canBeEdit || _isMouseWithinScope))

{

IsEditing = true;

}

}

}

#endregion

#region Public Properties

#region IsEditing

public static DependencyProperty IsEditingProperty =

DependencyProperty.Register(

"IsEditing",

typeof(bool),

typeof(EditBoxBase),

new FrameworkPropertyMetadata(false));

public bool IsEditing

{

get { return (bool)GetValue(IsEditingProperty); }

private set

{

SetValue(IsEditingProperty, value);

_adorner.UpdateVisibilty(value);

}

}

#endregion

#endregion

#region Private Methods

private void OnTextBoxKeyDown(object sender, KeyEventArgs e)

{

if (IsEditing && (e.Key == Key.Enter || e.Key == Key.F2))

{

IsEditing = false;

_canBeEdit = false;

}

}

private void OnTextBoxLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)

{

IsEditing = false;

}

#endregion

#region private variable

private EditBoxAdorner _adorner;

private FrameworkElement _textBox;

private bool _canBeEdit = false;

private bool _isMouseWithinScope = false;

#endregion

}

}

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.