I am trying to capture the value of the selected item in a list box, and store that value into my database. My goal is when I choose "Male" (or "Female") the "Male" (or "Female") will be added to the Gender [column] in the database but I don't know how to do it.
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;
}
}
My ListPicker list:
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;
}
My Add Button: I dont know what can be filled in ???????????????
Member is the table. Column is FullName | Address | Gender
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);
}
}
And my selection changed in LIstpicker:
private void ListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var samplesex =(GenderPicker)ListPicker.SelectedItem;
var selectedsex = (samplesex.sex).ToString();
}
Have you tried? Found on stackoverflow.com
or in your example:
you could/should make a property to have a default value so you don't receive NullReference exceptions if ListPicker has a NULL selected.