1

I'm writing a dialog box in WPF that I'd like to enter from the edge of the screen and animate to the center when it is shown. The dialog will need to stay positioned in the center of the screen even when the window is resized. This seems like it should be dead-simple to do in WPF, and yet I still haven't thought up a reliable way of accomplishing this effect. I'd prefer to use a pure "layout" approach as opposed to calculating the dialog's position using a binding.

In HTML/CSS, I would simply animate the dialog's left CSS property from 0' to 50%... is there a similar way to accomplish this effect in WPF?

4

1 回答 1

1

为了达到这个效果,我编写了自己的Panel子类,它的行为类似于Canvas但接受百分比偏移而不是绝对偏移。这使我可以UserControl在屏幕的 0% 到 50% 之间设置“模态”动画。

这是 Github 上的课程。

用法类似于Canvas

<Window>
    <local:PercentagePanel>
        <Button local:PercentagePanel.Left=".10" local:PercentagePanel.Bottom=".5"
         Width="100" Height="40">Hello world!</Button>
    </local:PercentagePanel>
</Window>

结果:

百分比面板示例

默认情况下,PercentagePanel根据每个孩子的适当边缘定位其孩子(类似于 CSS 定位)。还可以通过属性PercentagePanel相对于每个孩子的中心定位其孩子:PositioningMode

<local:PercentagePanel PositioningMode="Center">

这是上例中更改PositioningMode属性的结果:

使用 PositioningMode= 的 PercentagePanel 示例

于 2014-06-23T01:25:07.743 回答