开发者

Where and how should I implement ScrollViewer in my XAML?

开发者 https://www.devze.com 2023-01-25 09:29 出处:网络
So if (roughly) my XAML tree is as so: <TabControl Name=\"tab1\"> <TabItem Header=\"Untitled\" Name=\"tabMain\">

So if (roughly) my XAML tree is as so:

<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>

Putting the scrollviewer around my listbox did nothing. Placing the ControlTemplate as you see above also does nothing. My grid's width/height (which as you see is set my listboxitem template) dynamically expands and shrinks down but when it expands beyond the size of the window, still, no scroll bar.


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消