How to set the selecteditem of a listbox in WP7?

Go To StackoverFlow.com

0

How do I set the selected item of a listbox? When I use SelectedValue, SelectedItem, SelectedIndex, it does nothing. When I use UpdateLayout( ) afterwards, it does nothing. ScrollIntoView( item ) does not appear to do anything.

This is the XAML code for my Listbox. I use a Setter to set the orientation of the listbox to horizontal

<ScrollViewer HorizontalScrollBarVisibility="Visible" Name="DetailedWebViewContainer" VerticalScrollBarVisibility="Disabled" Grid.Row="2">
        <ScrollViewer.Resources>
            <Style TargetType="ListBox">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"/>
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ScrollViewer.Resources>
        <ListBox Name="WebScrollView" SelectionMode="Multiple" >
            <ListBox.Items>
                <phone:WebBrowser Name="LeftBrowser" MinWidth="460" Height="1500" IsHitTestVisible="False" Margin="10, 0, 10, 0"/>
                <phone:WebBrowser Name="MiddleBrowser" MinWidth="460" Height="1500" IsHitTestVisible="False" Margin="10,0,10,10"/>
                <phone:WebBrowser Name="RightBrowser" MinWidth="460" Height="1500" IsHitTestVisible="False" Margin="10,0,10,0" />
            </ListBox.Items>                
        </ListBox>
    </ScrollViewer>

Am I missing something? Greetz GeekPeek

2012-04-04 07:07
by GeekPeek
Why do you think that item is not selected? What do you get when call SelectedIndex after tap on some item? Please, provide more info; maybe some xaml or code will be helpfu - Ku6opr 2012-04-04 07:24
@Ku6opr I have added some XAML cod - GeekPeek 2012-04-04 07:28
@Ku6opr you are right, the selected item is set when I don't put in the horizontal stuff. ( I did not mention this as the code was provided by Microsoft, so I did not think this would be the cause ). So then, my next question is: how to make a listbox Horizontally orientated, while still being able to set the selected item - GeekPeek 2012-04-04 07:31
ListBox in ScrollViewer? You should put ItemsPanelTemplate inside of ListBox.ItemsPanel and remove ScrollViewer at al - Ku6opr 2012-04-04 07:31
Right, thanks alot! this works fine! But I used the scrollviewer so the user can scroll between the items, which, apart from the selectedItem functioned fine. I now have placed the ItemPanelTemplate, like you said, inside the ItemPanel of the listbox, but cannot any longer scroll between items. Should I still use a scrollviewer for this, or create something else - GeekPeek 2012-04-04 07:41
Of course, you also need to enable horizontal scrolling and disable vertica - Ku6opr 2012-04-04 07:46
That's just great, when I place it inside a scrollviewer, the SetItem does not work anymore =P Any idea on that? Sorry for the many questions, I'm just new to XAML = - GeekPeek 2012-04-04 07:52


2

This should work:

<ListBox Name="WebScrollView" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Auto" SelectionMode="Multiple" >
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.Items>
        <phone:WebBrowser Name="LeftBrowser" MinWidth="460" Height="1500" IsHitTestVisible="False" Margin="10, 0, 10, 0"/>
        <phone:WebBrowser Name="MiddleBrowser" MinWidth="460" Height="1500" IsHitTestVisible="False" Margin="10,0,10,10"/>
        <phone:WebBrowser Name="RightBrowser" MinWidth="460" Height="1500" IsHitTestVisible="False" Margin="10,0,10,0" />
    </ListBox.Items>                
</ListBox>
2012-04-04 08:16
by Ku6opr
Thanks a bunch! This works like a charm - GeekPeek 2012-04-04 08:23
Ads