Windows Phone添加到列表问题

时间:2013-02-08 00:50:46

标签: windows-phone-7 listbox add

(Windows Phone 7 SDK) 嗨, 我有一个名为 CTransactionList 的ListBox,并使用数据绑定将一些项添加到此列表框中。在这里,我有一个类来评估数据绑定。(我认为我的列表框&#39的XAML代码不需要,因为我的问题是由于一些编码问题而出现的)

public class CTransaction
    {
        public String Date1 { get; set; }
        public String Amount1 { get; set; }
        public String Type1 { get; set; }
        public CTransaction(String date1, String amount1, String type1)
        {
            this.Date1 = date1;
            this.Amount1 = amount1;
            switch (type1)
            {
                case "FR":
                    this.Type1 = "Images/a.png";
                    break;

                case "TA":
                    this.Type1 = "Images/b.png";
                    break;

                case "DA":
                    this.Type1 = "Images/c.png";
                    break;
            }
        }
    }

这里我有一个函数,当一个移动完成时,这个函数运行;(这个函数应该在函数运行时添加新项目)

List<CTransaction> ctransactionList = new List<CTransaction>();//Define my list

    public void movecompleted()
    {


        String DefaultDate = "";
        String DefaultAmount = "";
        String RandomType = "";

        DefaultDate = nameend.Text;
        DefaultAmount = diffend.Text;
        RandomType = "FR";

        ctransactionList.Add(new CTransaction(DefaultDate, DefaultAmount, RandomType));

        CTransactionList.ItemsSource = ctransactionList;
    }

第一次移动完成时,它会将所需元素添加到我的列表中。但是对于下一次,它不会添加到我的列表中。旧的保持存在。我通过将列表定义添加到我的函数中来尝试这种格式,如:

    public void movecompleted()
    {

        List<CTransaction> ctransactionList = new List<CTransaction>(); //List definition in function
        String DefaultDate = "";
        //...Same

}

这一次,它用新的项目替换了我当前的项目。不要追加到列表的末尾。 (两种方式,我的列表中有一个项目,而不是更多)我怎样才能每次都附加到列表中?我哪里错了?

这是我的调试报告。根据我在调试观察器中的观察,ctransactionList对象和CTransactionList ListBox都有所需的项目。唯一的问题是,即使CTransactionList具有从ctransactionList对象检索到的资源,CTransactionList也无法正确刷新。

以下是我的相关列表框的XAML代码。(如果需要)

<Grid>
                <ListBox Name="CTransactionList" Margin="0,0,0,0"  >
                    <ListBox.ItemTemplate >
                        <DataTemplate>
                            <Button Width="400" Height="120"  >
                                <Button.Content >
                                    <StackPanel Orientation="Horizontal" Height="80" Width="400">
                                        <Image Source="{Binding Type1}" Width="80" Height="80"/>
                                        <StackPanel Orientation="Vertical" Height="80">
                                            <StackPanel Orientation="Horizontal" Height="40">
                                                <TextBlock Width="100" FontSize="22" Text="Name:" Height="40"/>
                                                <TextBlock Width="200" FontSize="22" Text="{Binding Date1}" Height="40"/>

                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" Height="40">
                                                <TextBlock Width="100" FontSize="22" Text="Difficulty:" Height="40"/>
                                                <TextBlock Width="200" FontSize="22" Text="{Binding Amount1}" Height="40"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </StackPanel>
                                </Button.Content>
                            </Button>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

            </Grid>

提前致谢。

2 个答案:

答案 0 :(得分:1)

正如您所正确提到的,它与您的XAML无关。问题在你的代码中。

第一个简单修复可能会在设置新源之前清除ItemsSource,如此

CTransactionList.ItemsSource = null;
CTransactionList.ItemsSource = ctransactionList;

这样,您将清除现有的数据绑定并将新列表强制执行到ListBox。

另一个可以解决的问题是,

“将您的List更改为ObservableCollection。因为ObservableCollection扩展了INotifyPropertyChanged,因此能够自动更新ListBox”

List<CTransaction> ctransactionList = new List<CTransaction>();//Change this to below

ObservableCollection<CTransaction> ctransactionList = new ObservableCollection<CTransaction>();//Define my collection

答案 1 :(得分:0)

但是之前的断点

CTransactionList.ItemsSource = ctransactionList;

并运行该功能两次。你现在在ctransactionList有两件物品吗? 我怀疑绑定失败,ctransactionList实际上正在增加

相关问题