2013-03-05 18 views
0

我有一個列表控件,每個項目包含兩個圖像和文本。點擊要隱藏的每個項目或顯示選定列表項目上的選定圖像。如何在Windows Phone中的項目列表控件中顯示和隱藏圖像

這裏是XAML代碼片段:

  <ListBox x:Name="list" SelectionChanged="list_SelectionChanged" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding Name}"/> 
         <Image Source="{Binding ImagePath}" Stretch="None"/> 
         <Image Source="{Binding ImagePath}" Stretch="None" 
           Visibility="{Binding ImageVisibility, 
          Converter={StaticResource boolVisibilityConverter}}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

C#代碼:

dataSource = new ObservableCollection<ImageData>() 
     { 
     new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = false}, 
     new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = true}, 
     new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = true}, 
     new ImageData(){Name = "User2:", ImagePath="/Images/user2.png", ImageVisibility = true} 
     }; 

列表選擇更改事件:

 private void list_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     ((ImageData)(((object[])(e.AddedItems))[0])).ImageVisibility = false; 
     list.UpdateLayout(); 
    } 

的ImageData類:

public class ImageData 
    { 
     public string ImagePath { get; set; } 
     public string Name { get; set; } 
     public bool ImageVisibility { get; set; } 
    } 

圖像可視性轉換器:

public class BooleanToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     bool flag = false; 
     if (value is bool) 
     { 
      flag = (bool)value; 
     } 
     else if (value is bool?) 
     { 
      bool? nullable = (bool?)value; 
      flag = nullable.HasValue ? nullable.Value : false; 
     } 
     return (flag ? Visibility.Visible : Visibility.Collapsed); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return ((value is Visibility) && (((Visibility)value) == Visibility.Visible)); 
    } 

} 

請幫我實現這樣的功能。

+0

請詳細說明,更多一點點你正在嘗試去做。我不明白你爲什麼每個項目有兩個相同的圖像,哪一個是你想要隱藏什麼時候。 – 2013-03-05 19:33:13

回答

0

您需要使用INotifyPropertyChanged接口http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

例如這樣:根據你的任務

public class ImageData : INotifyPropertyChanged 
{ 
    private bool _imageVisibility; 
    public string ImagePath { get; set; } 
    public string Name { get; set; } 
    public bool ImageVisibility 
    { 
     get 
     { 
      return _imageVisibility; 
     } 
     set 
     { 
      _imageVisibility = value; 
      OnPropertyChanged("ImageVisibility"); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

完全改變,你可以看到here (dropbox)

+0

謝謝流明!這是一個回答。 – Picks 2013-07-26 09:23:08

相關問題