【问题标题】:Adding and updating a databound listbox C# windows phone添加和更新数据绑定列表框 C# windows phone
【发布时间】: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


    【解决方案1】:

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

    对 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
            });
        }
    }
    

    事物是非静态的,而 List 是 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 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
            });
        }
    }
    

    【讨论】:

    • 如果您真的想要一个全局列表,请使用单例模式。向 ShoppingListItems 添加一个静态 get 属性,例如 ShoppingListItems.Instance,它返回一个 ShoppingListItems 对象。然后在 ShoppingListItems 中将构造函数设为私有并设为私有静态 _instance = new ShoppingListItems()。然后让 ShoppingListItems.Instance 返回 _instance。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      谢谢你这么多 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"
      

      【讨论】:

        猜你喜欢
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        • 2014-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        相关资源
        最近更新 更多