我有一個WPF UserControl
(在ElementHost
之內),ScrollViewer
包含ItemsControl
。 HorizontalScrollbarVisibility
設置爲Auto
,所以如果不需要滾動,ScrollBar
會被隱藏。WPF Scrollviewer當ScrollBar可見時,DesiredSize不會增加
我的要求是,如果ScrollBar
顯示/隱藏,ElementHost
會相應地調整它的高度。爲了實現這一點,我正在聽SizeChanged
事件,我得到ScrollViewer
的DesiredSize
在EventHandler
,然後我通過DesiredSize.Height
到ElementHost
。
- 2.
的一種方式,其工作原理:隨着ScrollBar
可見的(情況1),我擴大了我的窗前,直到ItemsControl
的所有項目都可見,在ScrollBar
消失, ElementHost
適應高度降低(情況2)。 DesiredSize
實際上在ScrollBar
隱藏的時刻變小了。
另一種方式,但它不起作用:與ScrollBar
不可見(情況2),我減少我的窗口大小,直到ScrollBar
是必要的,並且出現。 DesiredSize
保持不變,ElementHost
不調整(情況3)。
任何想法?
這是Scrollviewer
的XAML,一些MVVM的東西,不過不要讓她掛在這,點真的是,爲什麼DesiredSize
的ScrollBar
出現在不增加嗎?爲什麼它只收縮?
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden" >
<i:Interaction.Behaviors>
<beh:HeightChangedBehavior HeightChangedCommand="{Binding HeightChangedCommand}" />
</i:Interaction.Behaviors>
<ItemsControl ItemsSource="{Binding TabHeaders}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel IsItemsHost="True" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="models:TabHeaderButtonModel">
<RadioButton Content="{Binding Caption}" IsChecked="{Binding IsChecked, Mode=TwoWay}" GroupName="Tabs"
Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding}"
Style="{StaticResource TabHeaderToggleButtonStyle}">
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
的ScrollViewer風格(基本上默認WPF):
<Style x:Key="ScrollViewerStyle1" TargetType="{x:Type ScrollViewer}">
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid x:Name="Grid" Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle x:Name="Corner" Grid.Column="1" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Row="1"/>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="0" Margin="{TemplateBinding Padding}" Grid.Row="0"/>
<ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
<ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="0" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}" Style="{DynamicResource ScrollBarStyle1}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>