2015-07-19 1 views
0

Salut StackOverflow,C# StackPanel: objets anciens restent après les valeurs Mise à jour

je rencontre un problème avec une certaine partie de mon application Windows Store. Ceci est d'abord ce qui est affiché ... Initialized

et c'est ce qui se passe lorsque le bouton est cliqué Show More Clicked

Comme vous pouvez le voir, le spectacle plus bouton et la chaîne d'origine est toujours présent. J'ai essayé de faire le bouton "Show More" mettre à jour le TextBlock à une valeur "" et le bouton "Show More" est toujours là et cliquable. J'ai également essayé d'effacer le StackPanel et de rajouter les articles en vain.

Voici le code que je faisais ceci:

private async void Description_Loaded(object sender, RoutedEventArgs e) 
    { 
     await wiki; 
     if (wiki.Result.ContainsKey("real_name")) 
     { 
      TeamInfoTitle.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
      TeamDescription.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
      PopulateInfo(Description); 
     } 
     else if(wiki.Result.ContainsKey("current_members")) 
     { 
      CharacterInfoTitle.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
      Description.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
      PopulateInfo(TeamDescription); 
     } 
    }   
void expand_Click(object sender, RoutedEventArgs e) 
    { 
     HyperlinkButton button = (HyperlinkButton)sender; 
     button.Content = "Show Less"; 
     button.Click -= expand_Click; 
     button.Click += shrink_Click; 
     TextBlock text = (TextBlock)button.Tag; 
     text.Text = (string)text.Tag; 
    } 

    void shrink_Click(object sender, RoutedEventArgs e) 
    { 
     HyperlinkButton button = (HyperlinkButton)sender; 
     button.Content = "Show More"; 
     button.Click -= shrink_Click; 
     button.Click += expand_Click; 
     TextBlock text = (TextBlock)button.Tag; 
     string item = (string)text.Tag; 
     text.Text = item.Substring(0, item.LastIndexOf(" ", 150, 15)) + "..."; 
    } 

    private void PopulateInfo(Grid desc) 
    { 
     for (int i = 0; i < desc.RowDefinitions.Count; i++) 
     { 
      string value; 
      if (wiki.Result.TryGetValue((desc.Children[i] as TextBlock).Name, out value)) 
      { 
       FrameworkElement now = new TextBlock() { Text = value, FontSize = 24, TextWrapping = TextWrapping.Wrap }; 
       if (value.Length > 200) 
       { 
        TextBlock text = (TextBlock)now; 
        HyperlinkButton expand = new HyperlinkButton() { Content = "Show More", HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right, VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom }; 
        expand.Tag = text; 
        expand.Click += expand_Click; 
        StackPanel stack = new StackPanel(); 
        text.Tag = value; 
        text.Text = value.Substring(0, value.LastIndexOf(" ", 150)) + "..."; 
        stack.Children.Add(text); 
        stack.Children.Add(expand); 
        now = stack; 
       } 
       Grid.SetRow(now, i); 
       Grid.SetColumn(now, 1); 
       now.Margin = new Thickness(0, 0, 0, 10); 
       desc.Children.Add(now); 
      } 
      else 
       desc.Children[i].Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
     } 
    } 

Toute aide serait grandement appréciée! Remarque: Description et TeamDescription sont des grilles définies dans le code XAML.

+0

Etes-vous sûr que PopulateInfo n'est exécuté qu'une seule fois? Vous devriez poster une plus grande partie de votre programme. – swistak

Répondre

0

Il s'avère que le code XAML a déclenché l'événement chargé deux fois, merci swistak pour me mettre sur la bonne voie!