如何捕获列表框项的值

时间:2013-12-20 16:54:11

标签: c# windows-phone listpicker

我试图在列表框中捕获所选项的值,并将该值存储到我的数据库中。我的目标是当我选择“男性”(或“女性”)时,“男性”(或“女性”)将被添加到数据库中的Gender [列],但我不知道该怎么做。

MyDatabase的:

[Table]
    public class Member : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property, and database column.
        private int _ID;

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

        private string _FullName;

        [Column]
        public string FullName
        {
            get { return _FullName; }
            set
            {
                if (_FullName != value)
                {
                    NotifyPropertyChanging("FullName");
                    _FullName = value;
                    NotifyPropertyChanged("FullName");
                }
            }
        }



        private string _Address;
        [Column]
        public string Address
        {
            get { return _Address; }
            set
            {
                if (_Address != value)
                {
                    NotifyPropertyChanging("Address");
                    _Address = value;
                    NotifyPropertyChanged("Address");
                }
            }
        }

        private string _Gender;

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

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a 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 that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

    }
    public class GenderPicker
    {
        public string sex
        {
            get;
            set;
        }

    }

我的ListPicker列表:

public Add()
    {
        InitializeComponent();
        this.DataContext = App.MainViewModel;
        List<GenderPicker> picker = new List<GenderPicker>();
        picker.Add(new GenderPicker() { sex = "Male" });
        picker.Add(new GenderPicker() { sex = "FeMale" });
        this.ListPicker.ItemsSource = picker;


    }

我的添加按钮:我不知道可填写什么???????????????

会员是桌子。列是FullName |地址|性别

private void Add_Click(object sender, EventArgs e)
    {
        if ((addaddress.Text.Length > 0) && (addfullname.Text.Length > 0))
        {
            Member newInfo = new Member
            {
                FullName = addfullname.Text,
                Address = addaddress.Text,
                Gender = ????????????????

            };
            App.MainViewModel.Addinfo(newInfo);
        }
    }

我的选择在LIstpicker中改变了:

  private void ListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var samplesex =(GenderPicker)ListPicker.SelectedItem;
        var selectedsex = (samplesex.sex).ToString();

    }

3 个答案:

答案 0 :(得分:2)

你尝试过吗? Found on stackoverflow.com

(ListPicker.SelectedItem as GenderPicker).sex.ToString()

或在您的示例中:

private void Add_Click(object sender, EventArgs e)
{
    if ((addaddress.Text.Length > 0) && (addfullname.Text.Length > 0))
    {
        Member newInfo = new Member
        {
            FullName = addfullname.Text,
            Address = addaddress.Text,
            Gender = (ListPicker.SelectedItem as GenderPicker).sex.ToString()

        };
        App.MainViewModel.Addinfo(newInfo);
    }
}

你可以/应该使一个属性具有默认值,这样如果ListPicker选择了NULL,你就不会收到NullReference异常。

答案 1 :(得分:0)

你累了gender = picker.SelectedValue.ToString();

Here是有关在Windows Phone上使用ListBox控件的更多信息。

答案 2 :(得分:0)

这个答案是关于数据结构的。

为什么使用'GenderPicker'类来选择性别? GenderPicker类有功能吗?

为什么不使用枚举器?

public enum Gender: int
{
    Male=0,
    Female=1,
    Pat=-1
}

这将允许您只在数据库中存储一个int列。此外,这很容易转换回性别。

int GenderInt = Gender.Pat; // -1
Gender whatIsPat = (Gender)GenderInt; // Gender.Pat