2

所以,这是我第一次在 VS 2012 for windows 桌面上使用 wpf 应用程序。我将一些背景图像放入窗口。我在那个窗口上放了三个文本框和一个按钮。现在我遇到了一个问题,当我启动应用程序并使用鼠标更改窗口大小或全屏显示时,窗口会更改其大小,但文本框和按钮仍保持与以前相同的位置,并且它们并不真正适合那个设计。我希望我足够清楚。感谢您的帮助。

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="768" Width="1366
    ">

<Grid RenderTransformOrigin="0.495,0.498" Margin="0,10,0,0">
    <Grid.Background>
        <ImageBrush ImageSource="slike/education_board.jpg"/>
    </Grid.Background>
    <Grid HorizontalAlignment="Left" Height="209" Margin="209,211,0,0"        VerticalAlignment="Top" Width="396">

    <TextBox x:Name="tockeMaturaTextBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Bottom" Width="120" TextChanged="TextBox_TextChanged_1" IsManipulationEnabled="True" Margin="10,0,0,176"/>
    <TextBox x:Name="tretjiLetnikTextBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.899,3.574" Margin="10,63,0,0"/>
    <TextBox x:Name="cetrtiLetnikTextBox" HorizontalAlignment="Left" Height="23" Margin="10,115,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="361,224,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    <TextBlock x:Name="skupneTockeTextBlock" HorizontalAlignment="Left" Margin="323,133,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"      RenderTransformOrigin="-0.375,0.338" Height="73" Width="113"/>

    </Grid>
</Grid>
</Window>
4

1 回答 1

4

试试下面的 XAML:

    <Grid Margin="0,10,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="211"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.Background>
            <ImageBrush ImageSource="slike/education_board.jpg"/>
        </Grid.Background>
        <StackPanel Grid.Row="1" Grid.Column="1" >

            <TextBox x:Name="tockeMaturaTextBox" Margin="0,5" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Bottom" Width="120" TextChanged="TextBox_TextChanged_1" IsManipulationEnabled="True" />
            <TextBox x:Name="tretjiLetnikTextBox" Margin="0,5" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" />
            <TextBox x:Name="cetrtiLetnikTextBox" Margin="0,5" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
            <Button x:Name="button" Margin="5" Content="Button" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="75" Click="button_Click"/>
            <TextBlock x:Name="skupneTockeTextBlock" Margin="5" HorizontalAlignment="Left"  TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"       Height="73" Width="113"/>

        </StackPanel>
    </Grid>

WPF 使用基于容器的(相对)布局系统。通过相对于父网格精确定义它们的边距,您将文本框放置在绝对位置。在上面的示例中,它们被放置在StackPanel网格内的 a 中。

有了所有额外的边距和绝对宽度和高度,很难说出你想要的最终结果,但上面的 XMAL 应该会让你接近。您可以更改网格行和列的相对大小以获得所需的内容;或完全重新定义Grid.

我认为关于 WPF 的一个很好的教程是为了掌握基本概念: Silverlight/WPF 简介。这有点面向 Silverlight,但布局原则与 WPF 相同。视频中的实际训练从 8 点 17 分开始。

编辑 对于神奇的扩展和收缩内容,使用下面的代码:

    <Grid Margin="0,10,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.Background>
            <ImageBrush ImageSource="slike/education_board.jpg"/>
        </Grid.Background>
        <Viewbox Grid.Row="1" Grid.Column="1" >
            <StackPanel>
                <TextBox x:Name="tockeMaturaTextBox" Margin="0,5" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Bottom" Width="120" TextChanged="TextBox_TextChanged_1" IsManipulationEnabled="True" />
                <TextBox x:Name="tretjiLetnikTextBox" Margin="0,5" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" />
                <TextBox x:Name="cetrtiLetnikTextBox" Margin="0,5" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
                <Button x:Name="button" Margin="5" Content="Button" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="75" Click="button_Click"/>
                <TextBlock x:Name="skupneTockeTextBlock" Margin="5" HorizontalAlignment="Left"  TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"       Height="73" Width="113"/>
            </StackPanel>
        </Viewbox>
    </Grid>
于 2013-03-06T19:09:06.257 回答