更改窗口宽度后,高度会自动更改
本文关键字:高度 窗口 | 更新日期: 2023-09-27 18:11:45
当我创建新窗口时,我手动设置其宽度和高度,并正确显示。但是,如果我在调用.show()方法后尝试更改它,它的行为就不正确。例如,如果我改变宽度,高度会自动改变,反之亦然。
SizeToContent属性设置为Manual
请更新您的问题,提供两个窗口的xaml。因为我还不能评论…我将尝试用我的项目给你提供正确的答案:主窗口。Xaml代码:
<Window x:Class="TestingHeightWidth.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestingHeightWidth"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="300" SizeToContent="Manual">
<Grid>
<Button x:Name="behaviorbtn" Content="Test" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="75" Click="behaviorbtn_Click"/>
</Grid>
新窗口的Xaml:
<Window x:Class="TestingHeightWidth.testWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestingHeightWidth"
mc:Ignorable="d"
Title="testWindow" Height="300" Width="300" SizeToContent="Manual"> <!--Height and width values will be overwritten by the mainwindow's constructor-->
<Grid>
</Grid>
MainWindow背后的代码:
public partial class MainWindow : Window
{
testWindow testwnd = new testWindow();
bool frstClick = true;
public MainWindow()
{
InitializeComponent();
testwnd.Height = 100; //before .Show();
testwnd.Width = 200;
testwnd.Show();
}
private void behaviorbtn_Click(object sender, RoutedEventArgs e)
{
//After .Show();
if (testwnd.Height == 400 && testwnd.Width == 500 && frstClick == false) //Second check
{
testwnd.Height = 100;
testwnd.Width = 200;
}
if (testwnd.Visibility == Visibility.Visible && frstClick == true) //Runs first
{
testwnd.Height = 400;
testwnd.Width = 500;
frstClick = false;
}
}
}
这将引导您正确编辑高度和宽度属性。好运。