Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListBox Style Selected item on windows phone

i would like know how can i add a style when a item of the listbox is selected. I have the following listbox:

<ListBox x:Name="ListBoxDays" 
                        VerticalAlignment="Top" 
                        ItemTemplate="{StaticResource WeekDayTemplate}" 
                        ItemsSource="{Binding WeekDayList}"  /> 

And i also have a DataTemplate to the listbox.

<phone:PhoneApplicationPage.Resources> 
        <DataTemplate x:Key="WeekDayTemplate"> 
            <StackPanel x:Name="stackPanel" Orientation="Horizontal" Width="400" Margin="12,0,0,10" Height="100"  > 
                <StackPanel VerticalAlignment="Center" Orientation="Vertical"> 
                    <TextBlock Text="{Binding WeekDayName}" Style="{StaticResource PhoneTextExtraLargeStyle}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Foreground="{StaticResource PhoneRadioCheckBoxPressedBorderBrush}" UseLayoutRounding="True" /> 
                    <TextBlock Text="{Binding ShortDate}" Style="{StaticResource PhoneTextTitle2Style}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Foreground="{StaticResource PhoneBorderBrush}" Margin="25,0,12,0" />                     
                </StackPanel> 
            </StackPanel>            
        </DataTemplate>                 
    </phone:PhoneApplicationPage.Resources> 

At the moment when i select an item of the listbox no color change happens.

like image 494
Filipe Batista Avatar asked Feb 08 '12 15:02

Filipe Batista


1 Answers

You have to change the style of the template ListBoxItem which the ListBox generates for each of the items that it renders. Your updated template needs to customise the Selected visual state. You can then associate this new template with your ListBox via the ListBox.ItemContainerStyle property.

There is a good tutorial, with sourcecode to download, here:

http://windowsphonegeek.com/tips/How-to-customize-the-WP7-ListBox-Selected-Item--Part1-Control-Template

like image 177
ColinE Avatar answered Nov 23 '22 14:11

ColinE