2012-02-17 71 views
1

我希望这个问题不是愚蠢的,我试图寻找一个解决方案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的东西,没有任何运气。

任何想法,我需要做什么来更新列表框?如果任何人有一些技巧会使这项工作,我会很高兴!

回答

2

首先,我会建议不使用静态成员,它似乎没有工作更好。

小的改动ShoppingListItems:

public class ShoppingListItems 
{ 
    public ObservableCollection<Item> ItemCollection { get; set; } 

    public ShoppingListItems() 
    { 
     ItemCollection = new ObservableCollection<Item>(); 
     AddAnItem("test"); 
     AddAnItem("test2"); 
     AddAnItem("test3"); 
    } 

    public void AddAnItem(string name) 
    { 
     ItemCollection.Add(new Item() 
     { 
      ItemName = name 
     }); 
    } 
} 

东西不是静态的,而列表<>是的ObservableCollection <>代替。

在XAML:

<Grid> 
    <Grid.Resources> 
     <DataTemplate x:Key="itemLayout" DataType="{x:Type local:Item}"> 
      <TextBlock Text="{Binding ItemName}" FontSize="50" /> 
     </DataTemplate> 
    </Grid.Resources> 

    <ListBox x:Name="itemsListBox" Margin="2" ItemsSource="{Binding}" 
      ItemTemplate="{DynamicResource itemLayout}" 
      IsSynchronizedWithCurrentItem="True"> 
    </ListBox> 
</Grid> 

在代码隐藏(对应于XAML文件中的cs文件):

private ShoppingListItems _shoppingList; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     _shoppingList = new ShoppingListItems(); 
     itemsListBox.DataContext = _shoppingList.ItemCollection; 
    } 

编辑:

我做了什么将数据模板放在网格资源中,同时将数据模板作为列表框资源的一部分。据我所知,唯一真正的区别是如果模板是网格资源的一部分,它可以在多个地方使用。假设你想要两个购物清单,也许一个是你需要的东西,另一个是你已经购买的东西,另一个是你希望它们以相同的方式格式化,最好将模板放在网格中,这样两个清单都可以引用它。既然你只有一个列表,它可能并不重要,任何一种方式都可以。

至于Singleton模式,请参阅: Singleton on Wikipedia

最简单的方法将有你的ShoppingListItems是这样的:

public class ShoppingListItems 
{ 
    private static ShoppingListItems _instance = new ShoppingListItems(); 
    public ObservableCollection<Item> ItemCollection { get; private set; } 

    public static ShoppingListItems Instance { get { return _instance; } } 

    private ShoppingListItems() 
    { 
     ItemCollection = new ObservableCollection<Item>(); 
     AddAnItem("test"); 
     AddAnItem("test2"); 
     AddAnItem("test3"); 
    } 

    public void AddAnItem(string name) 
    { 
     ItemCollection.Add(new Item() 
     { 
      ItemName = name 
     }); 
    } 
} 
+1

如果你真的想要一个单一的全球列表中使用Singleton模式。向ShoppingListItems中添加一个静态get属性,如ShoppingListItems.Instance,它返回一个ShoppingListItems对象。然后在ShoppingListItems中使构造函数为private,并创建一个私有静态_instance = new ShoppingListItems()。然后让ShoppingListItems.Instance返回_instance。 – Zik 2012-02-18 00:41:50

1

您是否尝试将ItemCollection更改为ObservableCollection而不是List?

0

Thak你这么多ZIK,你的代码修改工作。在我的XAML代码中,我只更改为ItemSource =“{Binding}”。

我真的不理解你最后的评论寿。需要做一些谷歌的搜索,试图了解,寄托都我做的几乎是新的我:)

我对项目列表XAML代码是现在这个样子:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" DataContext="{StaticResource ShoppingListItems}"> 
     <ListBox x:Name="listBox1" Height="Auto" ItemsSource="{Binding}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Margin="5" > 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="Auto" /> 
          <ColumnDefinition Width="*" /> 
         </Grid.ColumnDefinitions> 
         <TextBlock x:Name="textBlock1" Text="{Binding ItemName}" FontSize="50" /> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

和主电网我有这个:

 <Grid.Resources> 
     <application:ShoppingListItems x:Key="ShoppingListItems" /> 
    </Grid.Resources> 

这似乎很好。但如果是更好的,我应该怎么变化,添加喜欢你的代码:

<DataTemplate x:Key="itemLayout" DataType="{x:Type local:Item}"> 

ItemTemplate="{DynamicResource itemLayout}" 
IsSynchronizedWithCurrentItem="True" 
相关问题