在代码隐藏中调整WPF窗口的大小

时间:2017-08-21 17:50:00

标签: wpf vb.net

我想在代码中调整WPF窗口的大小。点击后我有一个Button缩小尺寸,但它没有,除非我先点击窗口边框

<Window x:Name="Window_3" x:Class="Window3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Window3"  SizeToContent="WidthAndHeight" >

<Viewbox Stretch="Uniform">
    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Label x:Name="ShowWIDTH" Content="Current width" Grid.Column="0"  
     Background="Gainsboro"  />

    <Button   Grid.Column="1" Background="YellowGreen"  Content="REDUCE" 
    Name="WidthButton" Height="Auto" Width="Auto" ></Button>
</Grid>
</Viewbox>
</Window>

VB代码是

Public Class Window3
    Private Sub WidthButton_Click(sender As Object, e As RoutedEventArgs) Handles WidthButton.Click
        If Width > 200 Then Width = Width * 0.95
        ShowWIDTH.Content = Width.ToString
    End Sub
    Private Sub Window3_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Width = 900
        ShowWIDTH.Content = Width.ToString
    End Sub
End Class

0 个答案:

没有答案