【问题标题】:C# StackPanel: Old Items Remain After Updating ValuesC# StackPanel:更新值后保留旧项目
【发布时间】:2015-07-19 20:10:33
【问题描述】:

嘿,StackOverflow,

我的 Windows 应用商店应用的某个部分存在问题。这是最初显示的内容...

这就是单击按钮时发生的情况

如您所见,显示更多按钮和原始字符串仍然存在。我尝试让“显示更多”按钮将 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 中定义的网格。

【问题讨论】:

  • 你确定PopulateInfo 只运行一次吗?您应该发布程序的大部分内容。

标签: c# button textblock stackpanel


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 2013-09-07
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2014-09-02
    • 2016-04-26
    • 2020-01-18
    • 1970-01-01
    相关资源
    最近更新 更多