0

因此,如果(大致)我的 XAML 树是这样的:

<TabControl Name="tab1">
            <TabItem Header="Untitled" Name="tabMain">
                <Canvas Name="canvasTest" DockPanel.Dock="Right">
                <local:VisualsHost Canvas.ZIndex ="99" x:Name="vshMain"></local:VisualsHost>
                    <ListBox Name="lstTiles" DockPanel.Dock="Right" SelectionMode="Extended" PreviewMouseRightButtonDown="grdMain_MouseRightButtonDown" 
             PreviewMouseRightButtonUp="grdMain_MouseRightButtonUp" MouseDown="lstTiles_MouseDown" >
                    <ListBox.Template>
                        <ControlTemplate>
                            <ScrollViewer>
                                <ItemsPresenter />
                            </ScrollViewer>
                        </ControlTemplate>
                    </ListBox.Template>
                        <ListBox.ItemContainerStyle>
                            <Style>
                                <Setter Property="Grid.Row" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Content.Row}"/>
                                <Setter Property="Grid.Column" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Content.Column}"/>
                                <Setter Property="ListBoxItem.Height" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                AncestorType={x:Type Window}}, Path=lstboxHeight}" />
                                <Setter Property="ListBoxItem.Width" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                AncestorType={x:Type Window}}, Path=lstboxWidth}" />
                                <Setter Property="ListBoxItem.IsHitTestVisible" Value="True" />
                                <Style.Resources>
                                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" Opacity=".3" />
                                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                                </Style.Resources>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <Grid ShowGridLines="True" IsItemsHost="True" Background="{DynamicResource LoadedImage}" 
                      Name="grdMain"> 
                            </Grid>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                    </ListBox>
            </Canvas>
        </TabItem>
    </TabControl>

将滚动查看器放在我的列表框周围什么也没做。如上所示放置 ControlTemplate 也无济于事。我的网格的宽度/高度(如您所见,设置为我的 listboxitem 模板)动态扩展和缩小,但是当它扩展超出窗口大小时,仍然没有滚动条。

4

2 回答 2

2

Because the ListBox is within Canvas it will not have its size adjusted as the container resizes. The Canvas itself can extend beyond the bounds of it's container.

ListBox has ScrollViewer built in for when the list content exceeds the maximum size of the ListBox, but you will never exceed this size, since the ListBox will just grow because it is not constrained by the Canvas.

The DockPanel attached properties you are using will not do anything for the layout. I would suggest replacing the Canvas with a Grid container which will restrain the Listbox size.

于 2010-11-17T22:40:22.510 回答
0

Did you try putting a ScrollViewer around your Grid inside the ItemsPanelTemplate?

    <ScrollViewer>
        <Grid ShowGridLines="True" IsItemsHost="True" Background="{DynamicResource LoadedImage}"  
                  Name="grdMain">
        </Grid>
    </ScrollViewer>
于 2010-11-17T22:41:00.763 回答