简体   繁体   English

如何在选择更改Wp8上更改列表框项目的图像

[英]How to change image of listbox item on selection change Wp8

I have to show a listbox which have UI similar as below: 我必须显示一个列表框,其UI如下所示:

在此处输入图片说明

On selection change of list box item I can change the background and foreground of list box item. 在选择列表框项目时,我可以更改列表框项目的背景和前景。 But i need to change the image also. 但我还需要更改图像。 On selection change the image should change and when i select other item the previous item image should get reset and next selected item image should get change. 在选择更改时,图像应更改,并且当我选择其他项目时,上一个项目图像应被重置,而下一个所选项目图像应被更改。

在此处输入图片说明

Is it possible? 可能吗? How can I do this? 我怎样才能做到这一点?

EDIT 1: the data template I am using for 编辑1:我正在使用的数据模板

<DataTemplate x:Key="SideListTemplate">
        <StackPanel Orientation="Vertical" Margin="0" Height="73">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="10">
                <Image Width="50" VerticalAlignment="Center"
                       Height="50" Source="{Binding OptionImageSource}"></Image>
                <TextBlock Margin="20,0,0,0" VerticalAlignment="Center"
                           Text="{Binding OptionName}" FontSize="25"></TextBlock>
            </StackPanel>
            <Image Height="1" Source="/Assets/Images/SideBar/CL_divider_line.png"></Image>
        </StackPanel>
    </DataTemplate>

and the style I am using is: 我正在使用的样式是:

<Style x:Key="SelectedItemStyle" TargetType="ListBoxItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames
                                Storyboard.TargetName="ContentContainer"
                                Storyboard.TargetProperty="Foreground"
                                Duration="0">
                                            <DiscreteObjectKeyFrame  KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="#3A3939"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames
                                Storyboard.TargetName="ContentContainer"
                                Storyboard.TargetProperty="Foreground"
                                Duration="0">
                                            <DiscreteObjectKeyFrame  KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="#FFFFFF"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames
                                Storyboard.TargetName="border"
                                Storyboard.TargetProperty="Background"
                                Duration="0">
                                            <DiscreteObjectKeyFrame  KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="#0659B7"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <StackPanel x:Name="border" Orientation="Horizontal">
                            <ContentControl x:Name="ContentContainer" 
                                            ContentTemplate="{TemplateBinding ContentTemplate}" 
                                            Content="{TemplateBinding Content}" 
                                            Foreground="{TemplateBinding Foreground}" 
                                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                            Margin="{TemplateBinding Padding}" 
                                            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

The xaml code is: xaml代码是:

<ListBox Name="listSideOptions"                       
                     ItemContainerStyle="{StaticResource SelectedItemStyle}"                          
                     ItemTemplate="{StaticResource SideListTemplate}">
            </ListBox>

Thanks Deepti 谢谢Deepti

try this 尝试这个

private void listname_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
         (((sender.Content as StackPanel).Children[0] as StackPanel).Children[0] as Image).Source 
         = //Whatever the new source is;

         listSideOptions.SelectedIndex = -1;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM