2015-04-27 44 views
0

我有列表框中的一些聯繫人編號在列表框中現在我想添加顯示和隱藏按鈕到列表框中的每個聯繫人項目當用戶按下隱藏按鈕接點都會被隱藏,當用戶按下顯示按鈕的接觸將是展示和反之亦然我如何在WP8做.. 我用下面的代碼,請幫助我..如何添加顯示和隱藏按鈕與圖像在Windows Phone 8的列表框中的項目

     </Grid.ColumnDefinitions> 
         <StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10"> 
          <Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="5, 0, 0, 0"> 
           <Image Source="{Binding ImageUrl, Converter={StaticResource kconverter}}" Width="48" Height="48" Stretch="Fill"/> 
          </Border> 
          <TextBlock x:Name="item_name" Text="{Binding Name, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="10, 0, 0, 0" /> 
         </StackPanel> 

         <StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10"> 
          <Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="355, 0, 0, 0"> 
           <Image Name="onimg" Visibility="Visible" Source="/Assets/Images/blue.button.png" Width="85" Height="20" Tap="Image_TapOn" Stretch="Fill"/> 
          </Border> 

         </StackPanel> 
         <StackPanel Orientation="Horizontal" Margin="0, 10, 0, 10"> 
          <Border BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" Margin="355, 0, 0, 0"> 
           <Image Name="offimg" Visibility="Collapsed" Source="/Assets/Images/setting-h.png" Width="85" Height="48" Tap="Image_TapOff" Stretch="Fill"/> 
          </Border> 

         </StackPanel> 
        </Grid> 
       </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

回答

0

嗨使用此代碼希望它適用於你。 注:綁定唯一的ObservableCollection到列表框控件使用INotifyPropertyChanged的

XAML:

<Grid Grid.Row="1"> 

     <ListBox x:Name="lstContact"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <Border Background="#404040" 
           Height="45" 
           Width="60" 
           Tag="{Binding ContactId}" 
           Tap="_Collapsed"> 
          <TextBlock Text="-" 
             FontSize="24" 
             VerticalAlignment="Center" 
             TextAlignment="Center" 
             Foreground="White" /> 
         </Border> 
         <Border Name="BrCount" 
           Width="160" 
           Height="45" 
           BorderBrush="#404040" 
           BorderThickness="0" 
           Margin="10,0"> 
          <TextBlock Name="LblCont" 
             Text="{Binding ContactName}" 
             FontSize="24" 
             VerticalAlignment="Center" 
             TextAlignment="Center" 
             Foreground="#404040" 
             Visibility="{Binding _Visibility}" /> 
         </Border> 
         <Border Background="#404040" 
           Height="45" 
           Width="60" 
           Tag="{Binding ContactId}" 
           Tap="_Visible"> 
          <TextBlock Text="+" 
             FontSize="24" 
             VerticalAlignment="Center" 
             TextAlignment="Center" 
             Foreground="White" /> 
         </Border> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

C#:

private ObservableCollection<Contact> _contact = new ObservableCollection<Contact>(); 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 
     _contact = new ObservableCollection<Contact>(); 
     for (int i = 0; i < 10; i++) 
     { 
      _contact.Add(new Contact() { ContactId = i, ContactName = "Name " + i, _Visibility = Visibility.Visible }); 
     } 
     lstContact.ItemsSource = _contact; 
    } 

    private void _Collapsed(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     Border bdr = (Border)sender; 
     _contact.Where(X => X.ContactId == Convert.ToInt32(bdr.Tag.ToString())).First()._Visibility = Visibility.Collapsed; 
    } 

    private void _Visible(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     Border bdr = (Border)sender; 
     _contact.Where(X => X.ContactId == Convert.ToInt32(bdr.Tag.ToString())).First()._Visibility = Visibility.Visible; 
    } 

    public class Contact : INotifyPropertyChanged 
    { 
     public int ContactId { get; set; } 

     public string ContactName { get; set; } 

     public Visibility _visibility; 

     public Visibility _Visibility 
     { 
      get { return _visibility; } 
      set { SetProperty(ref _visibility, value); } 
     } 

     protected void SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] String propertyName = null) 
     { 
      if (!object.Equals(storage, value)) 
      { 
       storage = value; 
       if (PropertyChanged != null) 
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     protected void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = null) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 
+0

感謝您的幫助,先生.. – user

+0

先生,我有一個更多的問題,我想添加一個列表框選定的項目,請檢查此鏈接 http://stackoverflow.com/questions/29844360/wp8-cant-show-listbox-item-after-using-hardware-back-button – user

相關問題