简体   繁体   中英

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

I have the following listpicker in my 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>

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.

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

In this case lets say the item you recieved from the database is of type Category and you bind that to the ListPicker. When someone selected an item, you would simply access it like so:

Categories selectedCategory = CategoryPicker.SelectedItem as Categories;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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