2012-02-17 72 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" 
相關問題