添加和更新数据绑定列表框C#windows手机



我希望这个问题不是愚蠢的,我已经尝试了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);

这个列表工作得很好,并且在listBox中显示"test",但添加到列表中不起作用,我试着查看了一些NotifyPropertyChanged的东西,但没有成功。

你知道我需要做什么来更新listBox吗?如果有人能给我一些建议,我会非常高兴的!

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

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模式,请参见:维基百科上的Singleton

最简单的方法是让你的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
        });
    }
}

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

他给了你这么多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"

最新更新