0

我有这个代码:

  <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,但没有任何反应。

4

2 回答 2

0

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

var image = (Image)sender;
image.Source = new Uri("/AppName;component/Images/imageName.png", UriKind.Relative)

确保您要使用的图像已保存到您的图像文件夹

(将应用名称中的空格替换为 %20)

于 2013-07-25T08:45:23.443 回答
0

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

于 2013-07-24T21:56:46.803 回答