4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ListBoxをネストして利用する

4
Last updated at Posted at 2015-08-16

WPFではListBoxをネストして利用できる。ただ、子ListBox内でマウスホイールでスクロールしても親ListBoxまでスクロールイベントが伝わらない。
これは子ListBox内ScrollViewerでスクロールイベントを処理してしまっているためなので、子ListBoxからScrollViewerをとりのぞけばOK

↓こんな感じでStyleを定義しておいて、

    <Style x:Key="NestedListBoxStyle" TargetType="ListBox">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ItemsPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

こう使う。

    <ListBox ItemsSource="{Binding Persons}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock>Header</TextBlock>
                    <TextBlock Text="{Binding Name}" />
                    <ListBox ItemsSource="{Binding Children}" Style="{StaticResource NestedListBoxStyle}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}" />
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                        
                    </ListBox>
                    <TextBlock>Footer</TextBlock>
                </StackPanel>

            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

すると子ListBox内のスクロールイベントが親ListBoxに伝わるようになった。

追記(2015/08/16)

kiichi54321さんからコメントをいただきました。
ItemsControlを使用すればデフォルトのスタイルで親ListBoxのスクロールができるようです。
お好みの方法でどうぞ。

4
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?