因此,如果(大致)我的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模板)动态扩展和缩小,但当它扩展超出窗口大小时,仍然没有滚动条。
答案 0 :(得分:2)
因为ListBox在Canvas中,所以在容器调整大小时不会调整其大小。 Canvas本身可以超出其容器的范围。
ListBox内置了ScrollViewer,用于当列表内容超过ListBox的最大大小时,但是你永远不会超过这个大小,因为ListBox只会增长,因为它不受Canvas的约束。
您正在使用的DockPanel附加属性不会对布局执行任何操作。我建议用Grid容器替换Canvas,这将限制Listbox的大小。
答案 1 :(得分:0)
您是否尝试在ItemsPanelTemplate中围绕Grid放置ScrollViewer?
<ScrollViewer>
<Grid ShowGridLines="True" IsItemsHost="True" Background="{DynamicResource LoadedImage}"
Name="grdMain">
</Grid>
</ScrollViewer>