如何获取ListBox窗口中显示的WPF ListBox文本?

时间:2011-10-21 21:08:33

标签: wpf listbox

我正在尝试将ID值(使用ListBox.SelectedValuePath)设置为WPF ListBox,并在ListBox窗口中显示已声明的(日期)文本(使用ListBox.DisplayMemberPath)。问题是我无法从ListBox窗口获取文本!我使用ComboBox已经做了很多次,但它似乎不适用于ListBox。 ListBox没有ListBox.Text属性,从中可以获取ListBox窗口中显示的文本,在本例中为日期。这是我的代码(我试图从lstBidPeriods窗口获取文本,通过从lstBidPeriods查询数据库中所选出价期限的假期来填充第二个ListBox(lstHolidays):

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

请:我不需要任何约束,所以不要去那里。这应该是一个简单的程序,而不是任何复杂的UNECESSARY绑定。

1 个答案:

答案 0 :(得分:2)

请允许我解释一下您在代码中所做的一些事情,答案非常明确。

向列表框提供了“ListContent”对象的列表。

lstBidPeriods.DisplayMemberPath = "Text";
lstBidPeriods.ItemsSource = func.GetBidPeriods(....

因此,我假设您在ListContent类中有一个名为“Text”的公共属性。如果是,那么您的列表将会显示,列出BidPeriods的ID。大。

但你也指明了这个:

lstBidPeriods.SelectedValuePath = "Value";

通过这样做,您可以更改选择的工作方式。让我们来看看各种选择属性。

lstBidPeriods.SelectedItem - 返回所选的实际ListContent实例,因为记住您传递了ListContent实例列表。

lstBidPeriods.SelectedValue - 返回所选ListContent实例的“Value”属性 - 所以这是日期字符串

lstBidPeriods.SelectedIndex - 返回列表中所选项目的索引。

因此,要获取日期字符串,请使用SelectedValue,

if (DateTime.TryParse(lstBidPeriods.SelectedValue.ToString(), out date))
{
   ...
}

就是这样。