2013-07-24 58 views
0

我有這樣的代碼:Сhanging所選項目的列表框中(Windows phone)系統的圖像

<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,但沒有任何反應。

+0

我已編輯我的文章,所以它只改變挖掘項:)讓我知道它是否工作/不 – Newbie

回答

0

很難說沒有更多代碼的最佳方式,但一種方法是將圖像路徑的屬性添加到「教師」類並將圖像源綁定到它。然後單擊該項目時,更改教師對象中的圖像路徑。 (確保您的教師課程正確支持OnPropertyChanged()圖像路徑屬性)。

+0

我認爲使用圖像屬性爲每個老師字段和這個進一步的修改並不是最優的。我想在xaml中處理所有(例如處理IsSelected事件的觸發器)。 –

0

使用下面的代碼來改變在C#中的圖像源(只是把它所需的事件處理程序後)

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

確保您要使用的圖像保存到你的圖片文件夾

+0

此代碼更改ListBox上所有項目中的背景圖像。是否可以更改單獨的項目bg圖像? –

+0

我編輯了我的文章,所以只有輕拍圖像會改變。讓我知道它是否有效/沒有 – Newbie

相關問題