Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EditBox doesn't work well standalone #95

Closed
nick2893 opened this issue Jul 16, 2019 · 1 comment
Closed

EditBox doesn't work well standalone #95

nick2893 opened this issue Jul 16, 2019 · 1 comment

Comments

@nick2893
Copy link
Contributor

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

}

}

@grubioe
Copy link

grubioe commented Jul 22, 2019

Issue moved to dotnet/wpf #1335 via ZenHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants