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

时间:2014-04-08 05:34:50

标签: c# .net image windows-phone-8 listboxitem

我必须显示一个列表框,其UI类似如下:

enter image description here

在列表框项目的选择更改中,我可以更改列表框项目的背景和前景。但我也需要改变图像。在选择更改时,图像应该更改,当我选择其他项目时,前一个项目图像应该重置,下一个选择的项目图像应该更改。

enter image description here

有可能吗? 我怎么能这样做?

编辑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>

我正在使用的风格是:

<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>

xaml代码是:

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

由于 Deepti

1 个答案:

答案 0 :(得分:0)

试试这个

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;
    }