Home > Mobile >  WPF UserControl container to Drag Window
WPF UserControl container to Drag Window

Time:10-07

I am trying to make a container UserControl (like a border) which can drag the current Window when you hold left click inside the UserControl.

UserControl.XAML :

<UserControl.InputBindings>
    <MouseBinding Gesture="LeftClick" Command="{Binding DragWindowCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/>
</UserControl.InputBindings>

UserControl.CS (Code Behind) :

    public partial class DragWindowBorder : UserControl
    {
        #region Commands
        public BaseCommand<Window> DragWindowCommand
        {
            get => new BaseCommand<Window>(/*async*/(window) => {
                window.DragMove();
            });
        }
        #endregion

        public DragWindowBorder()
        {
            InitializeComponent();
        }
    }

BaseCommand.CS :

    public class BaseCommand<T> : ICommand
    {
        #region Variables

        private readonly Action<T> _execute;
        private readonly Func<T, bool> _canExecute;
        public event EventHandler CanExecuteChanged;

        #endregion

        #region Methods
        public BaseCommand(Action<T> execute) : this(execute, null) { }

        public BaseCommand(Action<T> execute, Func<T, bool> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            _execute = execute;
            _canExecute = canExecute;
        }

        public void OnCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute((T)parameter);
        }

        public void Execute(object parameter)
        {
            if (CanExecute(parameter) && _execute != null)
            {
                T param = default(T);
                try
                {
                    param = (T)parameter;
                }
                catch { }

                _execute(param);
            }
        }

        #endregion
    }

My problem is the following :

  • When I call my UserControl containing a TextBlock the drag works fine
  • When I call my UserControl containing a TextBox or a Grid for example, the drag doesn't work

MainWindow.XAML :

<!-- Working -->
<controls:DragWindowBorder>
       <TextBlock Text="test"/>
</controls:DragWindowBorder>

<!-- Not Working -->
<controls:DragWindowBorder>
       <TextBox Text="test"/>
</controls:DragWindowBorder>

<!-- Not Working -->
<controls:DragWindowBorder>
         <Grid>
               <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
               </Grid.ColumnDefinitions>
         </Grid>
</controls:DragWindowBorder>

For some reason my UserControl seems not being able to capture left clicks when containing certain controls.

Can you help me figure out how to fix this issue?

CodePudding user response:

I managed to solve my problem.

1 - For the TextBox example, the TextBox is over my UserControl so it is capturing the left click instead of my User control.

2 - For the Gid example, I need to set a background in my UserControl as so :

<UserControl x:Class="Default.Common.Controls.Views.DragWindowBorder"
             ...
             Background="Transparent">

By default UserControls do not have any background, so they can not capture mouse clicks if they are filled with empty components (like an empty grid column in my example).

  • Related