Windows phone 7 ListBox不会自动展开,动态数据通过';电话文本框';wp7

Windows phone 7 ListBox不会自动展开,动态数据通过';电话文本框';wp7,windows-phone-7,c#-4.0,mvvm,listbox,dynamic-data,Windows Phone 7,C# 4.0,Mvvm,Listbox,Dynamic Data,我想执行一些类似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 dec

我想执行一些类似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);

        }
    }
string[]内容;
公共主页()
{
初始化组件();
content=new string[11]{“伟大的故事会产生一些想法,帮助孩子们知道聪明和明智的决定是如何让人们摆脱危险和最棘手的处境的。”,
“没有美德比善良更高尚。青蛙王子”、“鸽子和蚂蚁”等故事教导他们,即使是对上帝创造的地球上的小生物,也要仁慈。”,
“诚实是成功的关键,诚实的人会得到应有的回报。这些故事表明,诚实是一种最美丽的品质,值得拥有、喜爱和欣赏。”,
“谦逊使我们心地善良,说话温和——这些美德在别人眼中永远被视为和珍视。”,
“通过这些常青树的故事,认识到努力工作的重要性。这些受欢迎的故事是对“努力工作是通往成功之路”这一理念的深入理解。”,
“如果你说的是真话,你的孩子也会说。这将有助于他们在社会和周围建立信任和声誉。”,
“勇敢的孩子可以轻松地跨越人生的每一个里程碑,那么我们为什么不帮助我们的孩子变得坚强,让其他人也能跟随呢?”,
“真正的友谊是上帝的珍贵礼物,是对团结、分享和关怀的承诺。”,
“理解团结的价值,克服任何困难,理解团队合作的意义,并成长为一个善良和坚强的人。”,
“服从长辈最终会对你有好处。像《三只山羊和狼》这样的故事可以帮助你的孩子理解服从的价值。”,
“贪婪导致毁灭。它导致更有害的痴迷。让我们通过这些伟大的故事来了解它。”;
填充_列表();
}
私有无效填充列表()
{           
for(int i=0;i
{
如果(!string.IsNullOrEmpty(phonetextbox.Text))
{
expander.Items.Add(phonetextbox.Text);
expander.IsExpanded=true;
//stkpanel_new.Height=stkpanel_new.Height+20;
}
}; 
stkpanel_new.Children.Add(电话文本框);
stkpanel_new.Children.Add(扩展器);
mainlist.Items.Add(stkpanel);
主列表.项目.添加(stkpanel_新);
}
}
但是,当用户在phonetextbox中输入文本并按下其图标时,我在动态注释插入上遇到了问题。注释刚刚添加到listbox第2个元素后面(意味着ExpanderView将被展开,但listbox其他元素不会动态调整它)

是我做错了什么,还是这是一个框架限制??帮助

问候


Pardeep Sharma

对于这种类型的动态高度,我建议使用longlistselector控件,该控件在方向和主容器使用网格中都很有用,它将根据内容的大小自行设置,因为默认情况下其高度为“自动”

用此控件替换操作处理程序应该可以解决您的问题:

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虚拟化的好处(列表框不会构建所有项目的所有ui元素,只是在视图中构建一个,或者更多,但是由于您自己创建它们,因此您失去了这一优势).

Hi Benoit,你无法理解我的问题,让我简单介绍一下“当我通过expander view输入多个元素时,它只是与下一个列表项重叠,这将导致一些奇怪的布局”。通过添加dispatcher,我们无法解决问题,而且通过MVVM n虚拟化,我将在单独的层上实现它。我实际上测试了你的代码and看到了你正在描述的同样的事情,并在我添加的地方添加了一个调度器。它正在为我解决问题,但可能你的设备上的问题并不完全相同……事实上,我刚刚看到你上传了整个项目并进行了测试,另一个问题是你没有使用最新版本的Windows phone toolkit(例如,你可以从nuget获得)只要升级它,它就会工作嗨,伙计们,我已经在下面的天空驱动器链接上分享了我的代码,请检查它并拯救我的生命:链接:文件夹名称:日记列表问候,Pardeep sharmaHi@techloverr,正如我上面提到的,我正在动态填充我的列表,使用longlistselector我们没有任何I