0

我想执行类似 fb 评论布局的操作。

在此处输入图像描述

我正在使用以下代码来执行此任务:

// 构造函数

    string []content;
    public MainPage()
    {
        InitializeComponent();

        content = new string[11] { "Great stories produce the ideas which help children to know how the cleverness and wise decisions take the people out of the dangerous and most tricky situations.", 
                                   "No virtue is as noble as kindness. The stories “Prince Frog”, “Dove and the ant” and others teaches them the importance of being kind to even to the small god made creatures of this earth.", 
                                   "Honesty is a key to success and honest people are duly rewarded. These stories show honesty is a most beautiful quality to be possessed, liked and appreciated.",
                                   "Humbleness makes us kind hearted and soft spoken- the virtues which will always be deemed and valued in the eyes of the others.",
                                   "Realize the importance of hard work through these evergreen stories. These popular stories are the inroads to the very roots of the concept “Hard work is a road to success.",
                                   "If you speak the truth, your children will speak too.  This would help them built the trust and reputation in their society and around them.",
                                   "Courageous child can easily cross each milestones of life with ease, then why not we too help our children to be strong for the others too to follow.",
                                   "True Friendship is a God’s precious gift and a commitment for togetherness, and sharing and caring.",
                                   "Understand the value of being united, to overcome any difficult situation and understand the meaning of working in a team and grow as a virtuous and a strong human being.",
                                   "Obeying the elders will ultimately be good for you.  Stories like \"Three goats and the Wolf\" can help your children understand the value of being obedient.",
                                   "Greediness leads to destruction. It causes obsession which is more harmful. Let us know how through these great stories."  };

        populate_list();

    }

    private void populate_list()
    {           
        for (int i = 0; i < content.Length; i++)
        {
            //mainlist.Items.Add(content[i]);
            var maintext = new TextBlock();
            maintext.Text = content[i];
            maintext.TextWrapping = TextWrapping.Wrap;

            StackPanel stkpanel = new StackPanel();
            stkpanel.Width = 480;
            stkpanel.Height = 124;

            stkpanel.Children.Add(maintext);

            var expander = new ExpanderView();

            var phonetextbox = new PhoneTextBox();
            phonetextbox.Hint = "Add a Comment";
            phonetextbox.ActionIcon = new System.Windows.Media.Imaging.BitmapImage(new Uri("/search.png", UriKind.RelativeOrAbsolute));

            StackPanel stkpanel_new = new StackPanel();
            stkpanel_new.Width = 480;

            phonetextbox.ActionIconTapped += (s, e) =>                 
            {
                if (!string.IsNullOrEmpty(phonetextbox.Text))
                {
                    expander.Items.Add(phonetextbox.Text);
                    expander.IsExpanded = true;

                    //stkpanel_new.Height = stkpanel_new.Height + 20;
                }
            }; 

            stkpanel_new.Children.Add(phonetextbox);
            stkpanel_new.Children.Add(expander);

            mainlist.Items.Add(stkpanel);
            mainlist.Items.Add(stkpanel_new);

        }
    }

但是当用户在 phonetextbox 中输入文本并按下其图标时,我遇到了动态评论插入问题,该评论刚刚添加到列表框第二个元素后面(意味着 ExpanderView 将被展开,但列表框的其他元素不会动态调整它)。

我做错了什么还是框架限制?帮助 。

问候

帕迪普·夏尔马

4

2 回答 2

0

对于这种类型的动态高度,我推荐 longlistselector 控件,它在方向和主容器中都很有用,使用 Grid 它将根据内容的大小自行设置,因为它的高度默认为“自动”

于 2013-09-18T10:04:32.070 回答
0

用这个替换你的动作处理程序应该可以解决你的问题:

phonetextbox.ActionIconTapped += (s, e) =>                 
        {
            if (!string.IsNullOrEmpty(phonetextbox.Text))
            {
                expander.Items.Add(phonetextbox.Text);
                this.Dispatcher.BeginInvoke(() => {
                       expander.IsExpanded = true;
                });

                //stkpanel_new.Height = stkpanel_new.Height + 20;
            }
        }; 

但你真的应该考虑使用 mvvm。您的实现的一大缺点(在许多其他架构优势中)是您将失去 UI 虚拟化的优势(ListBox 不会构建所有项目的所有 ui 元素,只是查看和再多一点,但是由于您是自己创造它们,所以您失去了这种好处)。

于 2013-09-18T14:51:39.217 回答