Search code examples
wpfdrag-and-dropmaterial-design

MaterialDesign PopUpBox Drag and Drop Wpf


I want to use portable MaterialDesign PopUpButton in the project. The code below for drag and drop works but there are two problems. PosButtonClick event does not work when I apply drag and drop functionality. The second problem disappears when dragged off the page. How can I keep the PopUpButton inside the page?

xaml Code

<Grid AllowDrop="True">

    <Grid Name="MainGrid" Margin="148,0,561,0" VerticalAlignment="Top" Height="162">
        <materialDesign:PopupBox MouseUp="PortableButton_MouseUp" MouseLeftButtonUp="PortableButton_MouseLeftButtonUp" MouseMove="PortableButton_MouseMove" Name="button1" Style="{StaticResource MaterialDesignMultiFloatingActionPopupBox}" 
                                         PlacementMode="BottomAndAlignCentres" ToolTipService.Placement="Right" 
                                         ToolTip="PopupBox, Style MaterialDesignMultiFloatingActionPopupBox">
            <StackPanel>
                <Button Name="SystemButton" ToolTip="System">
                </Button>
                <Button Name="TestButton" ToolTip="Test">
                </Button>
                <Button Name="PosButton" ToolTip="Pos" Click="PosButton_Click">

                </Button>
            </StackPanel>
        </materialDesign:PopupBox>
    </Grid>

</Grid>

MouseUp Code

e.MouseDevice.Capture(null);

MouseMove Code

if (e.LeftButton == MouseButtonState.Pressed)
        {
            // Capture the mouse for border
            e.MouseDevice.Capture(button1);
            System.Windows.Thickness _margin = new System.Windows.Thickness();
            int _tempX = Convert.ToInt32(e.GetPosition(this).X);
            int _tempY = Convert.ToInt32(e.GetPosition(this).Y);
            _margin = MainGrid.Margin;
            // when While moving _tempX get greater than m_MouseX relative to usercontrol 
            if (m_MouseX > _tempX)
            {
                // add the difference of both to Left
                _margin.Left += (_tempX - m_MouseX);
                // subtract the difference of both to Left
                _margin.Right -= (_tempX - m_MouseX);
            }
            else
            {
                _margin.Left -= (m_MouseX - _tempX);
                _margin.Right -= (_tempX - m_MouseX);
            }
            if (m_MouseY > _tempY)
            {
                _margin.Top += (_tempY - m_MouseY);
                _margin.Bottom -= (_tempY - m_MouseY);
            }
            else
            {
                _margin.Top -= (m_MouseY - _tempY);
                _margin.Bottom -= (_tempY - m_MouseY);
            }
            MainGrid.Margin = _margin;
            m_MouseX = _tempX;
            m_MouseY = _tempY;
        }

MouseLeftButtonUp Code

m_MouseX = e.GetPosition(this).X;
m_MouseY = e.GetPosition(this).Y;

Fixed the PopupBox not popping out of the window. I have provided a control according to the Width and Height of the outermost Grid. The only problem is that the Click event of the opened buttons does not work.

var ActuelHeight = OutGrid.ActualHeight;
var ActuelWidth = OutGrid.ActualWidth;

if (_tempX < ActuelWidth && _tempX > 0 && _tempY < ActuelHeight && _tempY > 0)
        {
            //MouseMove Code ...
        }

Solution

  • I solved the problem.

    For the click event not working issue, I used PreviewMouseLefButtonDown event instead of click event and the problem was solved.

    I added a control for the window overflow issue so that it doesn't work outside the window size. I have provided a control according to the Width and Height of the outermost Grid.

    var ActuelHeight = OutGrid.ActualHeight;
            var ActuelWidth = OutGrid.ActualWidth;
    
            if (_tempX < ActuelWidth && _tempX > 0 && _tempY < ActuelHeight && _tempY > 0)
            {
                //MouseMove Code ...
            }