为 WPF Viewbox 设置延迟?

Set a Delay to a WPF Viewbox?

我有一个 Window,里面有一个 Viewbox。根据我Viewbox的内容调整大小Window可以得到相当'stuttery'。我不需要 Viewbox 实时调整其内容的大小,一旦用户调整完 Window.

的大小,更新 Viewbox 就足够了

我自己在 google 上找不到关于这个主题的任何信息。

有没有办法'delay'我的Viewbox

编辑:如果不是,模拟此行为的最佳方法是什么?

我能想到的最好的办法是在视图框周围创建一个网格,为宽度和高度创建 2 个属性,使用双向绑定将它们绑定到 window 和网格,然后在绑定中设置延迟,因此网格和其中的视图框将在延迟后调整大小,但是由于属性,我必须为我的 window 设置一个预定义的起始大小。

您可以使用 Canvas 作为 ViewBox 的容器。 (直接容器必须是 Grid,它给 ViewBox 一个调整大小的边界。)

与 Grid 不同,Canvas 使用绝对定位,并且不随 Window 调整大小,它的子项也是如此。

<Grid x:Name="root">
    <Canvas>
        <Grid x:Name="innerGrid">
            <Viewbox>
                <Content here />
            </Viewbox>
        </Grid>
    </Canvas>
</Grid>

然后您可以控制何时调整 ViewBox 的大小(通过调整其直接容器的大小)。

以下代码灵感来自于评论。它使用一次性 Timer,当用户完成操作时定时器启动,并且在定时器间隔结束时调整大小。

System.Timers.Timer timer; //Declare it as a class member, not a local field, so it won't get GC'ed. 
public MainWindow()
{
    InitializeComponent();
    timer = new System.Timers.Timer(1000);
    timer.AutoReset = false; //the Elapsed event should be one-shot
    timer.Elapsed += (o, e) =>
    {
        //Since this is running on a background thread you need to marshal it back to the UI thread.
        Dispatcher.BeginInvoke(new Action(() => {
            innerGrid.Width = root.ActualWidth;
            innerGrid.Height = root.ActualHeight;
        }));
    };

    this.SizeChanged += (o, e) =>
    {
        //restart the time if user is still manipulating the window             
        timer.Stop(); 
        timer.Start();
    };
}