简体   繁体   中英

Popup with StaysOpen to false is not closing

I have a telerik grid view and when I right click the header, I'm showing a with a ListBox inside containing the list of columns.

The item template is redefined to show a check box so I can set the column visible or not. I can also drag/drop columns to reorder them.

So here is how I create my Popup:

var view = new ColumnsOrderer.ColumnsOrderer
            {
                DataContext = new ColumnsOrderer.ViewModelColumnsOrderer(Columns)
            };

var codePopup = new Popup
                    {
                        Child = view,
                        MaxHeight = 400,
                        StaysOpen = false,
                        Placement = PlacementMode.Mouse
                    };
codePopup.IsOpen = true;

Now everything seems to work correctly, but it's not. If I set columns visible or hidden and then click outside the popup, it closes correctly.

Though if I drag an item to reorder it, the popup seems to lose focus and then it won't close if I click outside the popup. I have to click back in the list box inside the popup and then it closes by clicking outside.

Here is my drag/drop events:

public ColumnsOrderer()
{
    InitializeComponent();
    InitialiazeListBoxDragDrop();
}

private void InitialiazeListBoxDragDrop()
{
    var itemContainerStyle = new Style(typeof(ListBoxItem));

    itemContainerStyle.Setters.Add(new Setter(AllowDropProperty, true));
    itemContainerStyle.Setters.Add(new EventSetter(PreviewMouseMoveEvent, new MouseEventHandler(OnMouseMove)));
    itemContainerStyle.Setters.Add(new EventSetter(DropEvent, new DragEventHandler(OnDrop)));

    listColumns.ItemContainerStyle = itemContainerStyle;
}

void OnMouseMove(object sender, MouseEventArgs e)
{
    if (e.OriginalSource is CheckBox || e.LeftButton == MouseButtonState.Released)
        return;

    if (sender is ListBoxItem)
    {
        var draggedItem = sender as ListBoxItem;
        draggedItem.IsSelected = true;

        DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
    }
}

void OnDrop(object sender, DragEventArgs e)
{
    if (!(sender is ListBoxItem))
        return;
}

An interesting thing is that if I remove the OnDrop handler, the problem is not there. I tried many ways to set back the focus to the popup, but it's not working.

Could anyone help me on that?

How about trying to re-focus your Popup control after the drag and drop operation?

void OnDrop(object sender, DragEventArgs e)
{
    if (!(sender is ListBoxItem))
        return;
    codePopup.Focus();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM