【问题标题】:How to read the selected item from a listpicker in WP7?如何从 WP7 的列表选择器中读取所选项目?
【发布时间】:2012-02-10 22:39:59
【问题描述】:

我的 XAML 中有以下列表选择器;

<toolkit:ListPicker Name="CategoryPicker" ItemsSource="{Binding Category}" CacheMode="BitmapCache" FullModeHeader="{Binding Path=Resources.TheHeader}" SelectedIndex="{Binding TheCurrentIndex, Mode=OneWay}" IsEnabled="{Binding IsViewEnabled}" TabIndex="0" Margin="12,229,12,-205" SelectionChanged="CategoryPicker_SelectionChanged">
<toolkit:ListPicker.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <TextBlock Text="{Binding CategoryDesc}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
        </StackPanel>
    </DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
    <DataTemplate>
        <StackPanel x:Name="item" Orientation="Horizontal" Margin="5, 24, 0, 24">
            <TextBlock Margin="15, 0, 0, 0" Text="{Binding CategoryDesc}" FontSize="40" TextWrapping="Wrap" />
        </StackPanel>
    </DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>

我已经能够通过 LINQ 从 SQL CE 数据表中填充列表选择器,但我正在努力检索所选项目的值。

我尝试了以下方法;

ListPickerItem selectedItem = CategoryPicker.ItemContainerGenerator.ContainerFromItem(this.CategoryPicker.SelectedItem) as ListPickerItem;

我认为我没有正确理解这一点,但我似乎无法阅读所选项目的文本值,一如既往地感谢所有帮助!

编辑

类别的原始表定义如下;

[Table(Name = "Categories")]
    public class Categories : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property and database column.
        private int _categoryId;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int CategoryId
        {
            get
            {
                return _categoryId;
            }
            set
            {
                if (_categoryId != value)
                {
                    NotifyPropertyChanging("CategoryId");
                    _categoryId = value;
                    NotifyPropertyChanged("CategoryId");
                }
            }
        }

        // Define item category: private field, public property and database column.
        private string _categoryDesc;

        [Column]
        public string CategoryDesc
        {
            get
            {
                return _categoryDesc;
            }
            set
            {
                if (_categoryDesc != value)
                {
                    NotifyPropertyChanging("CategoryDesc");
                    _categoryDesc = value;
                    NotifyPropertyChanged("CategoryDesc");
                }
            }
        }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify the page that a data context property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify the data context that a data context property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

Category 的属性是;

private ObservableCollection<DBControl.Categories> _category;
public ObservableCollection<DBControl.Categories> Category
    {
        get
        {
            return _category;
        }
        set
        {
            if (_category != value)
            {
                _category = value;
                NotifyPropertyChanged("Category");
            }
        }
    }

希望这会有所帮助。

【问题讨论】:

  • (CategoryItem)this.CategoryPicker.SelectedItem;

标签: c# windows-phone-7 xaml listpicker


【解决方案1】:

看起来您访问 SelectedItem 的方式不正确。基本上,当您在 WP7 中使用 SQL CE 并将从数据库收到的结果绑定到数据控件时,它正在存储该特定类型的对象。

在这种情况下,假设您从数据库中收到的项目是 Category 类型,并且您将其绑定到 ListPicker。当有人选择一个项目时,您只需像这样访问它:

Categories selectedCategory = CategoryPicker.SelectedItem as Categories;

【讨论】:

  • 我尝试了你所说的,但我收到错误“类别是一个'属性',但用作'类型'”。目前 Category 是我的应用程序中的一个属性,它引用了我的数据库中的 MyCategory 表。不知道该怎么做。我理解错误,但我不知道如何修复它。
  • 你用什么数据类型(类)来创建你的表?
  • 我已将表格 def 和属性 def 添加到原始问题的末尾,感谢您的帮助。
  • 我更新了我的答案。因此,由于 Categories 是您从数据库中收到并将其绑定到 ListPicker 的类型,那么当您选择它时,您必须将其转换回。
  • 我在代码中犯了几个错误,但进行了一些重新整理,一旦更改以下工作: DBControl.Category selectedCategory = CategoryPicker.SelectedItem as DBControl.Category;感谢您为我指明正确的方向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2015-03-12
相关资源
最近更新 更多