ListBox不会自动扩展动态数据通过'PhoneTextBox'wp7添加

时间:2013-09-18 05:49:41

标签: windows-phone-7 c#-4.0 mvvm listbox dynamic-data

我想执行类似fb评论布局的内容。

enter image description here

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

//构造函数

    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将被展开,但列表框中的其他元素不会动态调整它)。

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

此致

Pardeep Sharma

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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元素,只是视图和再多一点,但既然你创造了他们所有的自我,你就失去了这种好处。)