none
ListBox中のListBoxでスクロール不具合 RRS feed

  • 質問

  • お世話になっております。

    TV番組表のようなデータを扱うために、水平方向にアイテムを並べるListBoxに、垂直方向にアイテムを並べるListBoxを配置したいとします。

    また、ListBoxはカスタマイズしたいとします。下記は、それを意図して書いた基本形です。

    <Window x:Class="MyListBox.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Width="200" Height="150">
    
    	<Window.Resources>
    		<Style x:Key="MainListBox" TargetType="ListBox">
    			<Setter Property="Template">
    				<Setter.Value>
    					<ControlTemplate TargetType="ListBox">
    						<ScrollViewer >
    							<StackPanel IsItemsHost="True" Orientation="Horizontal"/>
    						</ScrollViewer>
    					</ControlTemplate>
    				</Setter.Value>
    			</Setter>
    		</Style>
    
    		<Style x:Key="SubListBox" TargetType="ListBox">
    			<Setter Property="Template">
    				<Setter.Value>
    					<ControlTemplate TargetType="ListBox">
    						<ScrollViewer>
    							<StackPanel IsItemsHost="True"/>
    						</ScrollViewer>
    					</ControlTemplate>
    				</Setter.Value>
    			</Setter>
    		</Style>
    
    	</Window.Resources>
    
      <Grid>
    		<ListBox Style="{StaticResource MainListBox}">
    			<ListBox Style="{StaticResource SubListBox}">
    				<ListBoxItem Height="40">item1</ListBoxItem>
    				<ListBoxItem Height="40">item2</ListBoxItem>
    				<ListBoxItem Height="40">item3</ListBoxItem>
    				<ListBoxItem Height="40">item4</ListBoxItem>
    				<ListBoxItem Height="40">item5</ListBoxItem>
    			</ListBox>
    			<ListBox Style="{StaticResource SubListBox}">
    				<ListBoxItem Height="40">ITEM1</ListBoxItem>
    				<ListBoxItem Height="40">ITEM2</ListBoxItem>
    				<ListBoxItem Height="40">ITEM3</ListBoxItem>
    				<ListBoxItem Height="40">ITEM4</ListBoxItem>
    				<ListBoxItem Height="40">ITEM5</ListBoxItem>
    			</ListBox>
    		</ListBox>
    	</Grid>
    </Window>
    
    

    item5をクリックしようとすると、スクロール位置がTOPに戻ってしまい、item5を選択できません。

    Style="{StaticResource SubListBox}" を指定しなければ、すなわち通常のListBoxだと問題ありません。

    どうすれば解決できるでしょうか?

    教えてください。よろしくお願いします。

     

    2010年7月25日 7:12

回答

  • SubListBox スタイル内の ControlTemplate で使っている ScrollViewer を無くせば、通常の ListBox のときと同じように、item5 を選択できるようにはなります。


    なかむら(http://d.hatena.ne.jp/griefworker)
    • 回答としてマーク Yo48 2010年7月27日 11:14
    2010年7月26日 22:00

すべての返信

  • SubListBox スタイル内の ControlTemplate で使っている ScrollViewer を無くせば、通常の ListBox のときと同じように、item5 を選択できるようにはなります。


    なかむら(http://d.hatena.ne.jp/griefworker)
    • 回答としてマーク Yo48 2010年7月27日 11:14
    2010年7月26日 22:00
  • なかむら様

    ありがとうございます。

    なるほど。ScrollViewerの意味を正しく理解できました。

     

    2010年7月27日 11:12