当我创建新窗口时,我手动设置其宽度和高度并正确显示。但是,如果我在调用.show()方法后尝试更改它,它就不会正常运行。例如,如果我改变宽度,高度会自动改变,反之亦然。
SizeToContent属性设置为Manual。
答案 0 :(得分:1)
请更新您的问题,为两个窗口提供xaml。 因为我还无法发表评论......我会尝试使用我的项目为您提供正确的答案: MainWindow.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;
}
}
}
这将引导您以正确的方式编辑高度和宽度属性。祝你好运。