I am SIMPLY trying to set an ID Value (using ListBox.SelectedValuePath) to a WPF ListBox and show the assoicated (Date) Text (using ListBox.DisplayMemberPath) in the ListBox Window. The problem is I am unable to GET the Text from the ListBox Window! I have done this numerous times using a ComboBox, but it does not seem to work with a ListBox. The ListBox does not have a ListBox.Text property from which to obtain the Text displayed in the ListBox Window, in this case a date. Here is my code (I am trying to get the Text from the lstBidPeriods Window to populate a second ListBox (lstHolidays) by querying the database for holidays for the date of the selected bid period, from the lstBidPeriods):
private void loadBidPeriods()
{
lstBidPeriods.DisplayMemberPath = "Text";
lstBidPeriods.SelectedValuePath = "Value";
//lstBidPeriods.ItemTemplate.RegisterName(
lstBidPeriods.ItemsSource = func.GetBidPeriods(func.SelectedYear, func.SelectedMonth);
}
private void lstBidPeriods_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
lstHolidays.ItemsSource = null;
DateTime date = DateConversions.MIN_DATE;
Console.WriteLine("SelectedItem= " + lstBidPeriods.SelectedItem.ToString());
Console.WriteLine("SelectedValue= " + lstBidPeriods.SelectedValue.ToString());
Console.WriteLine("Items[index]= " + lstBidPeriods.Items[lstBidPeriods.SelectedIndex].ToString());
if (DateTime.TryParse(lstBidPeriods.SelectedItem.ToString(), out date))
{
Console.WriteLine("BID date: " + date);
lstHolidays.DisplayMemberPath = "Text";
lstHolidays.SelectedValuePath = "Value";
lstHolidays.ItemsSource = func.GetBidPeriodHolidays(date);
}
Console.WriteLine("NOW date: " + date);
}
public ArrayList GetBidPeriods(short selYear, byte selMonth)
{
DataTable periods = new DataTable();
ArrayList list = new ArrayList();
try
{
SqlConnection conn = openNewConnection();
SqlCommand cmd = new SqlCommand("getBidPeriods", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@selMonth", SqlDbType.TinyInt);
cmd.Parameters.Add("@selYear", SqlDbType.SmallInt);
cmd.Parameters["@selMonth"].Value = selMonth;
cmd.Parameters["@selYear"].Value = selYear;
periods = GetDataTable(cmd);
foreach (DataRow row in periods.Rows)
{
DateTime date = new DateTime((short)row["Bid_Year"], (byte)row["Bid_Month"], (byte)row["Bid_Day"]);
list.Add(new ListContent(row["Bid_Id"].ToString(), date.ToShortDateString()));
}
conn.Close(); conn.Dispose(); cmd.Dispose();
}
catch (Exception e)
{
message = "Error Obtaining Bid Periods: " + e.Message.ToString();
FileWizard.writeDate(FileWizard.LOGFILE_DIRECTORY + "appExceptionLog_"
+ DateConversions.GetMonthName(now.Month) + now.Year.ToString() + ".log",
"User: " + GFIFunctions.GFIUser + "\tClass: GFIDBInterface\tFunction: GetBidPeriods"
+ "\tException: " + message);
MessageBox.Show("Notify System Administrator: " + message, "GFI RIDER Application Exception ");
}
return list;
}
public ArrayList GetBidPeriodHolidays(DateTime bidPeriodDateStartingWithDate)
{
DataTable holidays = new DataTable();
ArrayList list = new ArrayList();
try
{
SqlConnection conn = openNewConnection();
SqlCommand cmd = new SqlCommand("getBidPeriodHolidays", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@bidHolidayDatesGreaterThanOrEqualTo", SqlDbType.DateTime);
cmd.Parameters["@bidHolidayDatesGreaterThanOrEqualTo"].Value = bidPeriodDateStartingWithDate;
holidays = GetDataTable(cmd);
foreach (DataRow row in holidays.Rows)
{
list.Add(new ListContent(row["Bid_Holiday_Id"].ToString(), ((DateTime)row["Bid_Holiday_Date"]).ToShortDateString()));
}
conn.Close(); conn.Dispose(); cmd.Dispose();
}
catch (Exception e)
{
message = "Error Obtaining Bid Period Holidays: " + e.Message.ToString();
FileWizard.writeDate(FileWizard.LOGFILE_DIRECTORY + "appExceptionLog_"
+ DateConversions.GetMonthName(now.Month) + now.Year.ToString() + ".log",
"User: " + GFIFunctions.GFIUser + "\tClass: GFIDBInterface\tFunction: GetBidPeriodHolidays"
+ "\tException: " + message);
MessageBox.Show("Notify System Administrator: " + message, "GFI RIDER Application Exception ");
}
return list;
}
PLEASE: I do not need to BIND anything, so don’t go there. This SHOULD be a SIMPLE procedure, not anything complicated with UNECESSARY binding.
Allow me to explain a few of the things you are doing in your code, and the answer will be crystal clear.
You are provided a list of “ListContent” objects to the listbox.
So, I assume that you have a public property in the ListContent class called “Text”. If so, then your list will show up, listing the ids of your BidPeriods. Great.
But you also specified this:
By doing that you change how the selection works a little bit. Lets take a look at the various selection properties.
lstBidPeriods.SelectedItem – Returns the actual ListContent instance that is selected, because remember you passed a list of ListContent instances.
lstBidPeriods.SelectedValue – Returns the “Value” property of the selected ListContent instance – so this is the date string
lstBidPeriods.SelectedIndex – Returns the index of the selected item in the list.
So to get the date string, use the SelectedValue,
That’s it.