3

これは簡単なことだと思いますが、大変な時間を過ごしています。

C#でListBoxのscrollviewerへの参照を取得するにはどうすればよいですか?私は考えられるほとんどすべてを試しました。ListBoxはWPFカスタムコントロール内にあるため、Template.FindNameを使用してすべてのコントロールへの参照を取得します。私のリストボックスは次のようになります。

<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)asScrollViewerはnullになります。

何か案は?

4

6 に答える 6

5

おそらく、ScrollViewerへの参照をすぐに取得しようとします。ロードされたイベントでコードを移動して、それでもnullが返されるかどうかを確認してください。

customControl / formコンストラクターで:

this.Loaded += MainWindow_Loaded;

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
   var x = PART_SoundList.Template.FindName("Scroller", PART_SoundList);
}
于 2010-10-18T21:36:41.743 に答える
2

上記の XAML は、CustomControl の ControlTemplate の一部であると想定していますよね? また、OnApplyTemplate() メソッドでコントロール パーツを取得していると思いますよね? この場合、ScrollViewer を見つける前に PART_SoundList.ApplyTemplate() を強制的に呼び出す必要があると思います。したがって、カスタム コントロールのコードは次のようになります。

public class MyControl : Control
{
    private ListBox lb;
    private ScrollViewer scroller;

    static MyControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        lb = this.Template.FindName("PART_SoundList", this) as ListBox;
        lb.ApplyTemplate();
        scroller = lb.Template.FindName("Scroller", lb) as ScrollViewer;
    }
}
于 2010-10-18T21:33:17.823 に答える
1

元の質問への回答を求めてここに来た人のために:

C# の場合

ScrollViewer sv = FindVisualChild<ScrollViewer>(MyListBox);

またはVBで

Dim sv As ScrollViewer = FindVisualChild(Of ScrollViewer)(MyListBox)

XAML の場所

<ListBox x:Name="MyListBox">
</ListBox>
于 2016-05-26T08:05:15.840 に答える
1

Visual Tree への再帰呼び出しを使用して、ツリーから任意の Visual を取得します。

public static ChildItem FindVisualChild<childItem>(DependencyObject obj) where ChildItem : DependencyObject

        {

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)

            {

                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is ChildItem)

                    return (ChildItem)child;

                else

                {

                    ChildItem childOfChild = FindVisualChild<ChildItem>(child);

                    if (childOfChild != null)

                        return childOfChild;

                }

            }

            return null;

        }

これにより、Visual ツリーから指定されたタイプの Visual 要素を取得するための一般的な方法が提供されます。

于 2010-10-18T21:04:12.397 に答える
0

リファレンスを使用してビューポート サイズをスクロール/チェックする場合は、IScrollProviderで十分です。

コード ビハインドで次のようにアクセスできます (ロードされたイベントまで待機する Claudiu のポイントに注意してください)。

ListBoxAutomationPeer svAutomation = (ListBoxAutomationPeer)ScrollViewerAutomationPeer.CreatePeerForElement(PART_SoundList);
// not feeling creative with my var names today...
IScrollProvider scrollInterface = (IScrollProvider)svAutomation.GetPattern(PatternInterface.Scroll);

次に、いつでも好きなときに水平方向および垂直方向にスクロールして、心のコンテンツに合わせます。

于 2010-10-18T22:10:43.477 に答える