gpt4 book ai didi

wpf - 强制布局更新

转载 作者:行者123 更新时间:2023-12-04 14:01:16 24 4
gpt4 key购买 nike

如何强制布局测量更新?

我简化了布局,我有问题;当您第一次单击按钮时,您会得到一个测量值,第二次单击不同的测量值。

   private void Window_Loaded(object sender, RoutedEventArgs e)
{
var w = mywindow.ActualWidth;
gridx.Width = w;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
btn3.Width = 100;
var w = mywindow.ActualWidth;
gridx.Width = w - btn3.Width;
InvalidateArrange();
InvalidateMeasure();

MessageBox.Show(btn1.ActualWidth.ToString());
}

window
<Window x:Class="resizet.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Name="mywindow">

<DockPanel HorizontalAlignment="Stretch" LastChildFill="False">
<Grid HorizontalAlignment="Stretch" DockPanel.Dock="Left" Name="gridx">
<Button HorizontalAlignment="Stretch" Content="btn in grid" Click="Button_Click" />
</Grid>
<Button Name="btn2" Content="btn2" Width="0" DockPanel.Dock="Right" HorizontalAlignment="Left"></Button>
</DockPanel>
</Window>

最佳答案

这解决了这个问题:

btn3.Width = 100;    
btn3.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
var w = mywindow.ActualWidth;
gridx.Width = w - btn3.Width;

加上额外的
private static Action EmptyDelegate = delegate() { };

关于wpf - 强制布局更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16839057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com