我希望这个问题不是愚蠢的,我试图寻找一个解决方案5小时,并且大多数在这个网站上找到有用的东西,但我无法解决我的问题。添加和更新数据绑定列表框C#windows phone
我有我的物品类别中只有这个属性:
public string ItemName { get; set; }
和A类持有项目:
public class ShoppingListItems
{
public static List<Item> ItemCollection { get; set; }
public ShoppingListItems()
{
ItemCollection = new List<Item>();
AddAnItem("test");
}
public static void AddAnItem(string name)
{
ItemCollection.Add(new Item()
{
ItemName = name
});
}
}
而在我的XAML文件,结合相关的代码是:
xmlns:application="clr-namespace:ShoppingListTest"
<Grid.Resources>
<application:ShoppingListItems x:Key="ShoppingListItems" />
</Grid.Resources>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" DataContext="{StaticResource ShoppingListItems}">
<ListBox x:Name="listBox1" Height="Auto" ItemsSource="{Binding ItemCollection, Mode=TwoWay}">
<TextBlock x:Name="textBlock1" Text="{Binding ItemName}" FontSize="50" />
然后在我的MainPage.xaml.cs中,在KeyDown的方法中我有:
ShoppingListItems.AddAnItem(textBox1.Text);
该列表很好,并显示列表框中的“测试”,但添加到列表不起作用,我试图看看一些NotifyPropertyChanged的东西,没有任何运气。
任何想法,我需要做什么来更新列表框?如果任何人有一些技巧会使这项工作,我会很高兴!
如果你真的想要一个单一的全球列表中使用Singleton模式。向ShoppingListItems中添加一个静态get属性,如ShoppingListItems.Instance,它返回一个ShoppingListItems对象。然后在ShoppingListItems中使构造函数为private,并创建一个私有静态_instance = new ShoppingListItems()。然后让ShoppingListItems.Instance返回_instance。 – Zik 2012-02-18 00:41:50