在 C# 中获取对我的 WPF ListBox 的 ScrollViewer 的引用? [英] Get reference to my WPF ListBox's ScrollViewer in C#?

查看:27
本文介绍了在 C# 中获取对我的 WPF ListBox 的 ScrollViewer 的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这应该很容易,但我很难做到.

I think this should be easy but I'm having a tough time with it.

如何在 C# 中获取对 ListBox 的滚动查看器的引用?我已经尝试了几乎所有我能想到的东西.ListBox 位于 WPF 自定义控件中,因此我们使用 Template.FindName 来获取对所有控件的引用.我的列表框如下所示:

How can I get a reference to my ListBox's scrollviewer in C#? I've tried pretty much everything I can think of. The ListBox is in a WPF Custom Control so we use Template.FindName to get references to all our controls. My ListBox looks like this:

<ListBox x:Name="PART_SoundList" 
                                 ScrollViewer.CanContentScroll="False" 
                                 ScrollViewer.HorizontalScrollBarVisibility="Auto"  
                                 ScrollViewer.VerticalScrollBarVisibility="Hidden" Focusable="False" FocusVisualStyle="{x:Null}"
                                 HorizontalAlignment="Center" VerticalAlignment="Bottom"  BorderThickness="0" 
                                 ItemContainerStyleSelector="{StaticResource ListBoxItemAlternatingStyleSelector}"
                                 ItemsSource="{Binding}"  >

                                    <ListBox.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <WrapPanel Orientation="Vertical"  Height="850" Focusable="False" Panel.ZIndex="999"  >
                                                <WrapPanel.RenderTransform>
                                                        <TransformGroup>
                                                            <ScaleTransform CenterX="0" CenterY="0" ScaleX=".75" ScaleY=".57" />
                                                        </TransformGroup>
                                                    </WrapPanel.RenderTransform>
                                            </WrapPanel>
                                        </ItemsPanelTemplate>
                                    </ListBox.ItemsPanel>

                                    <ListBox.Template>
                                        <ControlTemplate>
                                            <ScrollViewer x:Name="Scroller" VerticalAlignment="Bottom" Focusable="False" Style="{StaticResource HorizontalScroller}"   >
                                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Focusable="False" Panel.ZIndex="999"  />
                                            </ScrollViewer>
                                        </ControlTemplate>
                                    </ListBox.Template>

                                </ListBox>

Template.FindName("Scroller",this) as ScrollViewer 结果为 null.

Template.FindName("Scroller",this) as ScrollViewer results in null.

有什么想法吗?

推荐答案

您可能试图过早获得对 ScrollViewer 的引用.尝试在加载的事件中移动代码并检查它是否仍然返回 null:

You probably try to get a reference to the ScrollViewer too soon. Try to move your code in the loaded event and check if it still returns null:

在您的 customControl/form 构造函数中:

in your customControl/form constructor:

this.Loaded += MainWindow_Loaded;

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
   var x = PART_SoundList.Template.FindName("Scroller", PART_SoundList);
}

这篇关于在 C# 中获取对我的 WPF ListBox 的 ScrollViewer 的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆