PDA

View Full Version : سوال: اشکال در استفاده از listbox wpf



کاربر مبتدی
شنبه 07 بهمن 1391, 10:47 صبح
من در دروس کتاب آقای هاشمیان سعی کردم تمام دروس را با wpf بروم ولی در درس سوئیچ خروجی درستی نگرفتم (در wpf) لطفاً راهنمایی کنید.

private void lstData_SelectedIndexChanged(object sender,
EventArgs e)
{
// Declare variables
string strName;
string strFavoriteColor = "";
// Get the selected name
strName =
lstData.Items[lstData.SelectedIndex].ToString();
// Use a Switch to get the favorite color
// of the selected name
١٣٠
switch(strName)
{
case "Bryan":
strFavoriteColor = "Madras Yellow";
break;
case "Stephanie":
strFavoriteColor = "Sea Blue";
break;
case "Cathy":
strFavoriteColor = "Morning Mist";
break;
case "Betty":
strFavoriteColor = "Passionate Purple";
break;
case "Edward":
strFavoriteColor = "Battleship Gray";
break;
}
// Display the favorite color of the selected name
MessageBox.Show(strName + "‘s favorite color is " +
strFavoriteColor, "Select Demo");
}

Mahmoud.Afrad
شنبه 07 بهمن 1391, 12:01 عصر
خط

strName = lstData.Items[lstData.SelectedIndex].ToString();

رو به اینصورت بنویس:

ListBoxItem lstItem = lstData.SelectedItem as ListBoxItem;
strName = lstItem.Content.ToString();


در ضمن این سوال رو میبایست در تالار wpf میپرسیدید.