WPF - 我如何让scollviewer采取行动?

时间:2015-07-17 11:08:51

标签: c# wpf scrollview

我有一个网格,在该网格中我有以下内容:

<TabControl Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="3">
    <TabItem Header="Weapons">
        <ScrollViewer>
            <DataGrid>
                ...
            </DataGrid>
         </ScrollViewer>
     </TabItem>
</TabControl>

所以,我显然已经拿出了显示我的问题所不需要的东西。当Datagrid填满时,它所需的空间显然会增长。我打算将它尽可能地填满,然后滚动浏览器将完成其余的工作。但是,它似乎认为它可以具有无限长度,并且滚动查看器实际上从不滚动。

我不想手动设置maxheight值,因为TabControl上方有移动内容,所以它会改变(加上窗口大小调整等)。如何让滚动查看器不会超出窗口/父网格的限制扩展到TabControl?

简明的“完整”xaml:

<Grid>
<Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
</Grid.RowDefinitions>

<Grid Grid.Row="0" Grid.Column="1">

    <Image Source="{StaticResource EmptySlot}" Margin="0" Width="34"/>
    <Image Source="{Binding AuraSlot, FallbackValue={StaticResource AuraSlot}}" Margin="0" Width="34"/>
</Grid>

<Popup AllowsTransparency="True" Placement="Relative" PlacementTarget="{Binding ElementName=MainWindowName}" IsOpen="{Binding WeaponTooltipOpen}" HorizontalOffset="{Binding WeaponOffsetX}" VerticalOffset="{Binding WeaponOffsetY}" PopupAnimation="Fade" OpacityMask="White" >
    ...
</Popup>

<Grid Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3">

    <Expander orizontalAlignment="Center">
        <Expander.Header>
            <TextBlock Text="Equipment Slots" HorizontalAlignment="Center" />
        </Expander.Header>

        <Grid Grid.Row="0" Grid.Column="1">
            ...
        </Grid>

    </Expander>
</Grid>

<TabControl Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="3">
    <TabItem Header="Weapons">
        <Grid>
            <ScrollViewer CanContentScroll="True" >
                <DataGrid Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{Binding GroupedWeapons}" AutoGenerateColumns="False" IsReadOnly="True" AlternatingRowBackground="Gainsboro" AlternationCount="2" RowHeaderWidth="0">
                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            ...
                        </GroupStyle>
                    </DataGrid.GroupStyle>

                    <DataGrid.Columns>
                        ...
                    </DataGrid.Columns>

                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <StackPanel>
                                ...
                            </StackPanel>
                        </DataTemplate>
                    </DataGrid.RowDetailsTemplate>

                </DataGrid>
            </ScrollViewer>
        </Grid>
    </TabItem>


</TabControl>
</Grid>

1 个答案:

答案 0 :(得分:1)

如果您需要垂直滚动条,请尝试此操作或更改为horizo​​ntalScrollBar。并且CanContentScroll属性为true。

<TabControl Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="3">
    <TabItem Header="Weapons">
       <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto"  CanContentScroll="True">
            <StackPanel Orientation="Vertical">
                <GridView>
                ...
                </GridView>
            </StackPanel>
        </ScrollViewer>
     </TabItem>
</TabControl>

尝试它是否有效,或者更新我。

如果您想要窗口的大小,请尝试使用窗口高度。它会膨胀直到达到窗户的大小。