0

当应用程序未在全屏模式下打开时,它工作得很好。但是,当我打开全屏时,我的按钮和“StudentsDataGrid”失去了它们的位置,我看不到它们在哪里。当我调整窗口大小时,按钮和 DataGrid 也会改变它们的位置。这里 xaml 是:

<Window x:Class="StudentsBoard.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    Title="MainWindow" Height="996" Width="1191" 
    xmlns:my="clr-namespace:Students" Loaded="Window_Loaded"        
    WindowStartupLocation="CenterScreen" WindowStyle="SingleBorderWindow"  
    ResizeMode="CanResize" SizeChanged="Window_SizeChanged" SizeToContent="Manual" 
    WindowState="Maximized">

    <Grid Name="MainGrid" DataContext="{StaticResource StudentViewSource}">

    <Button Content="..." Height="40" HorizontalAlignment="Left"      
    VerticalAlignment="Top" Margin="1079,132,0,785" Name="btnbar_edit_student" Style="{DynamicResource GlassButtonBar}" Width="40" Click="btnbar_edit_student_Click" />
    <Button Content="-" Height="40" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1079,192,0,725" Name="btnbar_del_student" Style="{DynamicResource GlassButtonBar}" Width="40" Click="btnbar_del_student_Click" />

    <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="177"           
    HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding Source= 
    {StaticResource StudentViewSource}}" Margin="868,60,0,0" Name="StudentsDataGrid"    
    RowDetailsVisibilityMode="VisibleWhenSelected" Width="192"   
    MouseLeftButtonUp="StudentsDataGrid_MouseLeftButtonUp">                        
          </DataGrid>
    </Grid>

我希望元素保持完全相同的位置。决定是什么?

4

1 回答 1

1

您可以通过使用HorizontalAllignment=StretchVerticalAllignment = Stretch用于网格中存在的控件来执行此操作。

对此的替代解决方案也可以为您的网格定义行和列。例如,`

            <ColumnDefinition Width="222*" />
            <ColumnDefinition Width="211" />
            <ColumnDefinition Width="159" />
            <ColumnDefinition Width="282*" />
        </Grid.ColumnDefinitions>

`

这种情况的最佳方法是使用 WPF 提供的面板布局。如DockPanel、StackPanel。

于 2015-03-10T17:00:39.107 回答