【发布时间】:2012-03-09 07:12:00
【问题描述】:
我希望这个问题不是愚蠢的,我已经尝试了 5 个小时来寻找解决方案,并且大部分都在这个网站上找到了有用的东西,但我无法解决我的问题。
我的项目类只有这个属性:
public string ItemName { get; set; }
还有一个存放物品的类:
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 的东西,但没有任何运气。
知道我需要做什么来更新列表框吗?如果有人有一些技巧可以使这项工作顺利进行,我将非常高兴!
【问题讨论】:
标签: list windows-phone-7 data-binding listbox