我希望我的DataGrid
占用可用窗口中的所有可用空间。但是,它并没有完全这样做:
(不幸的是,我不允许嵌入图片,所以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)之外,我没有做任何进一步的操作。我缺少什么?
答案 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
,因为默认情况下HorizontalAlignment
和VerticalAlignment
设置为Stretch
HorizontalContentAlignment
和VerticalContentAlignment
(在您的Xaml中)不参与安排 DataGrid
。
首先,应该看到DataGrid
的容器类型。如果是StackPanel
或DockPanel
之类的话,这就是问题所在。改变他们。但是,它是Grid
,DockPanel
,Border
,Window
......,你应该检查他们的属性(HorizontalAlignments等)以及他们的容器