C#StackPanel:更新值后旧项目仍然存在

时间:2015-07-19 20:10:34

标签: c# button textblock stackpanel

嘿那里有StackOverflow,

我的Windows Store应用程序的某个部分出现问题。这是最初显示的内容...... Initialized

这是单击按钮时发生的情况 Show More Clicked

如您所见,show more按钮和原始字符串仍然存在。我试过制作" Show More"按钮将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中定义的网格。

1 个答案:

答案 0 :(得分:0)

原来XAML代码触发加载的事件两次,感谢swistak让我走上正确的轨道!