0
WPF 4.5/C#如何在WPF ContentControl中查找ScrollViewer?
我有一個應用程序,我有幾個WPF Windows每個利用此自定義內容控件。我用它在XAML這樣的:
<ContentControl Name="myControl" Style="{StaticResource ReservedSpaceScrollBar}"
在後面的代碼,我需要能夠訪問的ScrollViewer裏面,所以我可以調用.ScrollToTop()
我試過這個,但它不起作用:
((ScrollViewer)this.myControl.FindName(「Scroll」))。ScrollToTop();
...但.FindName找不到ScrollViewer。我究竟做錯了什麼?如何使這項工作?
的XAML的樣式低於...
<Style TargetType="{x:Type ContentControl}" x:Key="ReservedSpaceScrollBar">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<ScrollViewer PanningMode="Both" VerticalScrollBarVisibility="Auto" x:Name="Scroll" FocusVisualStyle="{x:Null}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter />
<Border Width="{x:Static SystemParameters.VerticalScrollBarWidth}" x:Name="Placeholder" Grid.Column="1" />
</Grid>
</ScrollViewer>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ComputedVerticalScrollBarVisibility, ElementName=Scroll}" Value="Visible">
<Setter TargetName="Placeholder" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
這很好。謝謝! – Joey 2015-03-19 22:48:39