繁体   English   中英

C#StackPanel:更新值后仍保留旧项目

[英]C# StackPanel: Old Items Remain After Updating Values

嘿,StackOverflow,

我的Windows Store应用程序的某些部分出现问题。 最初显示的是... 已初始化

这就是单击按钮时发生的情况 显示更多点击

如您所见,显示更多按钮和原始字符串仍然存在。 我尝试使“显示更多”按钮将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;
        }
    }

任何帮助将不胜感激! 注意:Description和TeamDescription是XAML中定义的网格。

事实证明,XAML代码两次触发了加载的事件,感谢swistak将我带入正确的轨道!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM