Windows phone 7 更改列表框(Windows Phone)中所选项目的图像

Windows phone 7 更改列表框(Windows Phone)中所选项目的图像,windows-phone-7,silverlight-4.0,datatemplate,Windows Phone 7,Silverlight 4.0,Datatemplate,我有以下代码: <ListBox x:Name="lisbox1" Margin="-24,0,-9,0" CacheMode="BitmapCache" ItemsSource="{Binding Teacher}" HorizontalAlignment="Right" MouseLeftButtonUp="lisbox1_MouseLeftButtonUp" SelectionChanged="lisbox1_SelectionChanged"> <

我有以下代码:

  <ListBox x:Name="lisbox1" Margin="-24,0,-9,0" CacheMode="BitmapCache" ItemsSource="{Binding Teacher}" HorizontalAlignment="Right" MouseLeftButtonUp="lisbox1_MouseLeftButtonUp"  SelectionChanged="lisbox1_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0,0,0,-35">                            
                    <Image x:Name="image1" Stretch="Fill"  Source="ticket.png" Margin="-15,-16,3,21"/>                             
                    <TextBlock x:Name="asda" Margin="56,50,0,0" TextWrapping="Wrap" TextAlignment="Left" Text="{Binding lastname}" Style="{StaticResource PhoneTextLargeStyle}" Height="50" VerticalAlignment="Top" HorizontalAlignment="Left" Width="264" />
                    <TextBlock Margin="56,106,43,90" TextWrapping="Wrap" TextAlignment="Left" Text="{Binding firstname}" Style="{StaticResource PhoneTextLargeStyle}" />
                    <TextBlock Margin="0,50,43,147" Text="{Binding auditnumb}" Style="{StaticResource PhoneTextLargeStyle}" RenderTransformOrigin="0.515,-1.286" HorizontalAlignment="Right" Width="98" />
                    <Border Background="Silver" BorderThickness="1,1,1,1" Height="40" CornerRadius="20,20,20,20" Margin="56,3,60,0" VerticalAlignment="Top">
                        <TextBlock TextWrapping="Wrap" Text="{Binding subname}" TextAlignment="Center" VerticalAlignment="Top" Style="{StaticResource PhoneTextTitle3Style}"/>
                    </Border>
                    <Border BorderThickness="1" Height="40" Background="Green" Width="69" CornerRadius="25,25,25,25" Margin="316,154,61,41" Name="border1" >
                        <TextBlock Text="{Binding End}" TextWrapping="Wrap" TextAlignment="Center" Style="{StaticResource PhoneTextTitle3Style}" Width="69" Margin="-2,1,-3,-1"/>
                    </Border>
                    <Border BorderThickness="1" Background="Red" CornerRadius="25,25,25,25"  Name="border2" Height="40" Width="69" Margin="230,154,147,41" >
                        <TextBlock TextAlignment="Center" TextWrapping="Wrap" Text="{Binding Start}" Style="{StaticResource PhoneTextTitle3Style}" Width="69" Margin="-2,1,-3,1"/>
                    </Border>
                    <Border Background="WhiteSmoke" CornerRadius="40" BorderThickness="1" Height="40" HorizontalAlignment="Left" Margin="16,88,0,0" Name="border3" VerticalAlignment="Top" Width="40" >
                        <TextBlock Margin="5" Foreground="Black" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding PairNumber}" Width="28" RenderTransformOrigin="0.304,-0.462" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top"/>
                    </Border>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

从列表框中选择项目时如何更改图像?我尝试过使用VisualStateManager,但没有任何效果。

如果没有更多代码,很难判断最佳方法,但有一种方法是将图像路径的属性添加到“教师”类,并将图像源绑定到该类。然后,单击项目时,更改教师对象中的图像路径。确保教师类正确支持图像路径属性的OnPropertyChanged。

使用以下代码在C中更改图像源,只需将其放在所需的事件处理程序之后

var image = (Image)sender;
image.Source = new Uri("/AppName;component/Images/imageName.png", UriKind.Relative)
确保要使用的图像已保存到图像文件夹中


将应用程序名称中的空格替换为%20

我认为对每个教师字段使用图像属性,并且进一步修改不是最佳选择。我想在xamle.g中处理所有问题。处理IsSelected事件的触发器。此代码更改ListBox上所有项目的背景图像。是否可以更改单独的项目bg图像?我已经编辑了我的帖子,因此只有点击的图像会更改。让我知道它是否有效/我已经编辑了我的帖子,所以它只会更改点击的项目:让我知道它是否有效/无效