DataGrid在屏幕调整大小

时间:2017-02-19 14:17:28

标签: c# wpf datagrid resize

我希望我的DataGrid占用可用窗口中的所有可用空间。但是,它并没有完全这样做:

Not maximized
Maximized

(不幸的是,我不允许嵌入图片,所以StackOverflow会链接到它)

这是我整个Windows的XAML:

<Window x:Class="BPS.View.MainView"
        x:Name="MainViewWindow"
        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"
        xmlns:local="clr-namespace:BPS.View"
        xmlns:cal="http://www.caliburnproject.org"
        mc:Ignorable="d" Title="{Binding WindowTitle}"
        WindowStartupLocation="CenterScreen" Height="539.2" Width="768">

    <Grid x:Name="MainGrid" IsEnabled="{Binding MainGridIsEnabled}">
    <DataGrid x:Name="MainDataGrid"
                  cal:Message.Attach="[Event MouseRightButtonUp] = [Action DataGridRightMouseClickEvent($eventArgs)]"
                  ItemsSource="{Binding MainGridItemsSource}" AutoGenerateColumns="False" Margin="10,23,10.6,-477.6"
                  HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
                  HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=id}" Width="Auto" Header="ID" CanUserResize="True"
                                    IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding Path=name}" Width="Auto" Header="Name"
                                    CanUserResize="True" IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding Path=age}" Width="Auto" Header="Age"
                                    CanUserResize="True" IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

除了DataGrid中的这些属性(应该在全屏模式下最大化DataGrid)之外,我没有做任何进一步的操作。我缺少什么?

2 个答案:

答案 0 :(得分:0)

DataGrid直接放在窗口的根Grid元素中,然后删除边距。

然后它会填满整个窗口。您可以通过设置Background

DataGrid属性来确认这一点
<Window x:Class="WpfApplication1.Window1"
        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"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <DataGrid x:Name="MainDataGrid"
              Background="Green"
              ItemsSource="{Binding MainGridItemsSource}" AutoGenerateColumns="False"
              HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
              HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=id}" Width="Auto" Header="ID" CanUserResize="True"
                                IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding Path=name}" Width="Auto" Header="Name"
                                CanUserResize="True" IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding Path=age}" Width="Auto" Header="Age"
                                CanUserResize="True" IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

答案 1 :(得分:0)

这不是DataGrid,因为默认情况下HorizontalAlignmentVerticalAlignment设置为Stretch HorizontalContentAlignmentVerticalContentAlignment(在您的Xaml中)不参与安排 DataGrid

首先,应该看到DataGrid的容器类型。如果是StackPanelDockPanel之类的话,这就是问题所在。改变他们。但是,它是GridDockPanelBorderWindow ......,你应该检查他们的属性(Horizo​​ntalAlignments等)以及他们的容器