响应wpf窗口

时间:2015-09-28 00:03:48

标签: wpf xaml

我是WPF的新人,请记住这一点。我正在尝试让窗口响应调整大小,textboxesbuttons等控件是否可能随着窗口的增大或缩小而调整大小?

<Window x:Class="WPF_Working_Experimenet.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="216.586" Width="459.256">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="156*"/>
            <ColumnDefinition Width="295*"/>
        </Grid.ColumnDefinitions>
        <Label Content="username :" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="66,27,0,0" Height="30" Width="63" FontFamily="B Nazanin" FontSize="15" Grid.Column="1"/>
        <Label Content="password :" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="74,63,0,0" Height="30" Width="55" FontFamily="B Nazanin" FontSize="15" Grid.Column="1"/>
        <Label x:Name="lblwrong" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,150,0,0" ClipToBounds="True" Grid.Column="1" Height="26" Width="275"/>
        <Button x:Name="btnLogin" Content="Login" HorizontalAlignment="Left" VerticalAlignment="Top" Width="60" Grid.Column="1" Margin="211,103,0,0" Height="37" Background="White" Click="btnLogin_Click" BorderBrush="Red" FontFamily="B Nazanin" FontSize="15"/>
        <Image HorizontalAlignment="Left" Height="117" Margin="4,23,0,0" VerticalAlignment="Top" Width="178" Source="img/Ticket_5523675581838074942.png" Grid.ColumnSpan="2" RenderTransformOrigin="0.5,0.128"/>
        <Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="60" Grid.Column="1" Margin="146,103,0,0" Height="37" Background="#FFFDFDFD" BorderBrush="Red" Click="Button_btnExit" FontFamily="B Nazanin" FontSize="15"/>
        <TextBox x:Name="txtUsername" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="125" Grid.Column="1" Margin="146,34,0,0" BorderBrush="#FFF7311E"/>
        <PasswordBox x:Name="txtPassword" Grid.Column="1" HorizontalAlignment="Left" Margin="146,70,0,0" VerticalAlignment="Top" Width="125" Height="23" BorderBrush="#FFFD3306"/>

    </Grid>
</Window>

任何Reference/hintTutorial/link都很可爱。提前谢谢你

3 个答案:

答案 0 :(得分:5)

如果您希望控件增长/缩小,则必须设置边距/填充而不是显式宽度/高度。

很少有链接可以帮助您:

Layout with Absolute and Dynamic Positioning

How to make all controls resize accordingly proportionally when window is maximized?

答案 1 :(得分:0)

如果您明确设置#include <iostream> #include <cstring> #include <string> #include <cctype> using namespace std; int vowels(string s, int L) { int sum = 0; char str[s.length()-1]; strcpy(str,s.c_str()); if(str[L]) { tolower(str[L]); if(str[L]!='a'||str[L]!='e'||str[L]!='i'||str[L]!='o'||str[L]!='u') sum = 0; else sum = 1; return sum += vowels(s,L++); } else return 0; } Width属性,则不会调整控件大小。请尝试以下操作,我删除了HeightWidth属性,并将HeightHorizontalAlignment值更改为VerticalAlignment

Stretch

答案 2 :(得分:0)

        

        <ColumnDefinition />

        <ColumnDefinition />

        <ColumnDefinition />

    </Grid.ColumnDefinitions>
    <Image Source="View/Images/LoginImg.jpg" Stretch="Fill" Grid.Column="0" Grid.ColumnSpan="3">
        <Image.Effect>
            <BlurEffect Radius="8"/>
        </Image.Effect>
    </Image>
    <TextBox Grid.Column="0" Grid.ColumnSpan="3" BorderBrush="Transparent" BorderThickness="0" Background="Transparent" Foreground="White" HorizontalAlignment="Center"
             Height="78" Margin="122,10,45.6,0" TextWrapping="Wrap" Text="Library Management System " VerticalAlignment="Top" Width="594" FontSize="40" FontWeight="Bold" FontStyle="Normal">
        <TextBox.VerticalContentAlignment>Center</TextBox.VerticalContentAlignment>
        <TextBox.IsReadOnly>True</TextBox.IsReadOnly>
    </TextBox>
    <Canvas Grid.Column="1" HorizontalAlignment="Stretch"  Width="auto" Height="300" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Background="White">
        <Canvas.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="0.627"/>
                <TranslateTransform/>
            </TransformGroup>
        </Canvas.RenderTransform>
        <Label Content="Login" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Background="BurlyWood" FontSize="15" Height="52" Width="254">
            <Label.FontWeight>Bold</Label.FontWeight>
        </Label>
        <TextBox Height="24" Canvas.Left="20" TextWrapping="Wrap" Text="User Name" TextAlignment="Center" Canvas.Top="84" Width="120" Foreground="Gray"/>
        <PasswordBox Canvas.Left="20" Canvas.Top="136" Height="34" Width="120"/>
    </Canvas>
</Grid>

将水平和垂直对齐方式设置为拉伸,而不是设置小部件的宽度和高度即可解决问题。但是仍然可以根据需要设置边距。进行了尝试,并确保在更改窗口大小时,小部件将分别进行调整。