2015-07-19 9 views
0

嘿StackOverflow上後,C#的StackPanel:老項目,仍更新價值

我有我的Windows商店應用的特定部分的問題。這是最初顯示的內容... Initialized

,這是按鈕被點擊 Show More Clicked

時,你可以看到發生了什麼,更多的顯示按鈕和原始字符串仍然存在。我試圖讓「顯示更多」按鈕將TextBlock更新爲「」值,並且「顯示更多」按鈕仍然存在並且可點擊。我也嘗試清除StackPanel並重新添加項目無濟於事。

這裏是我用來做這個代碼:

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; 
     } 
    } 

任何幫助將不勝感激!注意:描述和TeamDescription是在XAML中定義的網格。

+0

你確定'PopulateInfo'只運行一次嗎?你應該發佈你的程序的更大一部分。 – swistak

回答

0

原來,XAML代碼兩次觸發加載的事件,謝謝swistak讓我走上正軌!