ListBox Style Selected item on windows phone

我与影子孤独终老i 提交于 2020-01-01 04:31:46

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/9196364/listbox-style-selected-item-on-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!