Shahar Prish
Shahar Prish

Reputation: 4847

How do I make a UWP Popup associated with a control programatically?

A behavior I am writing needs to associate a popup with a control - so that the position of the popup is always relative to that control.

In UWP, Popups do not have the placement property to play with. How do I then associate it with a control? (I know I can manually calculate the position, but that gets very complicated very quickly when you realize that resizing the window does not necesserily affect the control, and so it becomes very hard to know when to update the positon)

I cannot use flyouts because I need a popup that's not light-dismissable.

Upvotes: 0

Views: 583

Answers (1)

Sunteen Wu
Sunteen Wu

Reputation: 10627

How do I make a UWP Popup associated with a control programatically?

If you mean associate two controls code behind, I think the ExpressionAnimation is what you want.

Properties of the Popup control visual could be calculated based on another control by Expression you defined. Just for simple example, the following code snippet makes the Popup always has Offset.X to 50 based on another visual's Offset.

private void btnmain_Click(object sender, RoutedEventArgs e)
{
    _visualA.Offset = new System.Numerics.Vector3(400, 350, 350);
}

private void btntest_Click(object sender, RoutedEventArgs e)
{
    _visualA = ElementCompositionPreview.GetElementVisual(btnmain);
    _visualB = ElementCompositionPreview.GetElementVisual(pop); 
    compositor = _visualB.Compositor; 
    var expression = compositor.CreateExpressionAnimation("visualA.Offset.X + 50");
    expression.SetReferenceParameter("visualA", _visualA);
    _visualB.StartAnimation("Offset.X", expression);
}

For detail calculation please do it yourself based on your scenarios.More details please reference WindowsUIDevLabs.

Upvotes: 3

Related Questions