简体   繁体   English

如何从WP7中的listpicker读取所选项目?

[英]How to read the selected item from a listpicker in WP7?

I have the following listpicker in my XAML; 我的XAML中有以下listpicker;

<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>

I have been able to populate the listpicker from a SQL CE data table via LINQ, but I am struggling to retrieve the value of the selecteditem. 我已经能够通过LINQ从SQL CE数据表填充listpicker,但我正在努力检索selecteditem的值。

I have tried the following; 我试过以下几点;

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

I don't think I have understood this properly but I can't seem to to read the text value of the selected item, all help, as always, is appreciated! 我不认为我已经理解了这一点,但我似乎无法阅读所选项目的文本值,所有帮助,一如既往,表示赞赏!

edit 编辑

The original table definition for categories is below; 类别的原始表定义如下;

[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
    }

The property for Category is; 分类的属性是;

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

Hope this helps. 希望这可以帮助。

It doesn't look like your accessing the SelectedItem the right way. 它看起来不像您正确访问SelectedItem。 Basically when your working with SQL CE in WP7 and you bind results you recieved from the database to a data control it is storing the object of that specific type. 基本上,当您在WP7中使用SQL CE并将从数据库收到的结果绑定到数据控件时,它会存储该特定类型的对象。

In this case lets say the item you recieved from the database is of type Category and you bind that to the ListPicker. 在这种情况下,假设您从数据库中收到的项目是Category类型,并将其绑定到ListPicker。 When someone selected an item, you would simply access it like so: 当有人选择了某个项目时,您只需访问它:

Categories selectedCategory = CategoryPicker.SelectedItem as Categories;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM