を含むWPF UserControl
(の内部ElementHost
)ScrollViewer
がありItemsControl
ます。はHorizontalScrollbarVisibility
に設定されてAuto
いるため、スクロールが不要な場合は、ScrollBar
が非表示になります。
私の要件は、ScrollBar
が表示/非表示になった場合、ElementHost
それに応じて高さを調整することです。それを達成するために、私はSizeChanged
イベントを聞いています。私はのを取得しDesiredSize
、ScrollViewer
次にEventHandler
に渡しDesiredSize.Height
ますElementHost
。
2.3
。
1つの方法として、これは機能します。ScrollBar
表示されている場合(状況1)、ウィンドウを拡大して、すべてのアイテムItemsControl
が表示され、ScrollBar
消え、ElementHost
高さが低くなるように調整します(状況2)。隠されDesiredSize
た瞬間に実際に小さくなりました。ScrollBar
ただし、逆の場合は機能しません。表示されない場合(状況2)、aが必要になりScrollBar
、表示されるまでウィンドウサイズを縮小します。同じままで、調整されません(状況3)。ScrollBar
DesiredSize
ElementHost
何か案は?
これは、Scrollviewer
いくつかのMVVMのものを含むのxamlですが、これにこだわる必要はありません。要点は、実際には、表示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>