使用Windows Phone 8中的数据绑定将项目添加到列表中

时间:2014-03-01 09:32:49

标签: data-binding windows-phone-8

我正在尝试动态地将列表添加到项目中,作为学习数据绑定的一部分。但这些物品没有被添加。该应用程序非常简单,只需在列表中添加一个字符串即可。

XAML代码如下

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <ListBox x:Name="TotalItems" Grid.Row="0" ItemsSource="{Binding Items_OC}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" Width="440">
                        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Item : " VerticalAlignment="Top"/>
                        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Top" Margin="1,0,0,0" FontSize="{StaticResource PhoneFontSizeLarge}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            <TextBox x:Name="NewItem" Grid.Column="0" TextWrapping="Wrap" Width="359"/>
            <Button x:Name="Add" Grid.Column="1" Content="Add"/>

        </Grid>
    </Grid>`

XAML.CS代码如下

    public ObservableCollection<Items> items_OC;
    public ObservableCollection<Items> Items_OC
    {
        get
        {
            return items_OC;
        }
        set
        {
            if (items_OC != value)
            {
                MessageBox.Show("Item to added to collection");
                items_OC = value;
                NotifyPropertyChanged("Items_OC");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class Items : INotifyPropertyChanged, INotifyPropertyChanging
    {
        string name;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    NotifyPropertyChanging("Name");
                    name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        private void NotifyPropertyChanging(string propertyName)
        {

            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

    }
    private void Add_Click(object sender, RoutedEventArgs e)
    {
        Items it = new Items { Name = NewItem.Text };

        Items_OC.Add(it);
    }

感谢您的帮助..

2 个答案:

答案 0 :(得分:1)

Item.cs

public class Item
{
    public string Name { get; set; }
}

MainPage.xaml

<Grid x:Name="ContentPanel">
    <ListBox x:Name="TotalItems"
             ItemsSource="{Binding Items}"
             VerticalAlignment="Top">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="Item :" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <StackPanel Orientation="Vertical"
                VerticalAlignment="Center"
                Margin="0,10,0,0">
        <TextBox x:Name="NewItemTextBox" />
        <Button x:Name="AddNewItem"
                Content="Add"
                Click="AddNewItem_Click" />
    </StackPanel>
</Grid>

MainPage.cs

    public ObservableCollection<Item> Items { get; set; }

    public MainPage()
    {
        InitializeComponent();

        Items = new ObservableCollection<Item> { new Item { Name = "Roman" } };

        DataContext = this;
    }

    private void AddNewItem_Click(object sender, RoutedEventArgs e)
    {
        Items.Add(new Item { Name = NewItemTextBox.Text });
    }

简单易行,但不简单!

答案 1 :(得分:1)

您的代码似乎缺少的是设置DataContext。通常,绑定会解析DataContext属性的路径。以这种方式绑定ItemsSource属性时:

<ListBox x:Name="TotalItems" Grid.Row="0" ItemsSource="{Binding Items_OC}" ...>

应用程序将搜索Items_OC ListBox的{​​{1}}属性。目前,由于DataContextDataContext,因此无法找到该属性。您可以从C#代码中将null设置为DataContext文件,如下所示:

xaml.cs

或来自XAML:

this.DataContext = this;