您必须在代码隐藏中或通过 Visual Studio 或 Blend 中的事件面板订阅列表控件的 SelectionChanged 事件。在此事件的处理程序中,您会将所选项目转换为其实际类型:TB。然后你可以读/写它的属性,并且应该看到列表项中反映的变化。像这样的东西:
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
ListBox mylistbox;
// Constructor
public MainPage()
{
InitializeComponent();
// here, for illustration purposes, I have declared and instantiated a listbox in the code-behind,
// normally the declaration and initialization is done by auto-generated code,
// when you have dropped the list into your GUI via the designer...
// although the actual adding of items is either populated in code-behind
// or is accomplished by binding to a source in the designer.
this.mylistbox = new ListBox();
// add some items...
TB[] tbs = new TB[] { new TB(), new TB(), new TB() };
this.mylistbox.ItemsSource = tbs;
// subscribe to the SelectionChanged event...
this.mylistbox.SelectionChanged += new SelectionChangedEventHandler(mylistbox_SelectionChanged);
}
// respond to the SelectionChanged event...
void mylistbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TB tb = this.mylistbox.SelectedItem as TB;
if (tb == null) { return; }
// the 'as' operator allows for a safe way of testing what we are using before we use it...
// we passed the validation, so now we can do what we like with the type...
// the selected TB item is being operated upon here, but that does not stop it from being an item in the list.
// this means whatever we 'do' to it here, is actually 'doing' it to the selected item in the list.
tb.F_Color = "Yellow";
}
}
public class TB
{
public string F_Name { get; set; }
public string F_Color { get; set; }
}
}
编辑:
这是上述代码的修订版,针对触发“Hold”事件时所选项目变为空的事实量身定制:
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
private TB currentItem;
ListBox mylistbox;
// Constructor
public MainPage()
{
InitializeComponent();
// here, for illustration purposes, I have declared and instantiated a listbox in the code-behind,
// normally the declaration and initialization is done by auto-generated code,
// when you have dropped the list into your GUI via the designer...
// although the actual adding of items is either populated in code-behind
// or is accomplished by binding to a source in the designer.
this.mylistbox = new ListBox();
this.currentItem = null;
// add some items...
TB[] tbs = new TB[] { new TB(), new TB(), new TB() };
this.mylistbox.ItemsSource = tbs;
// subscribe to the SelectionChanged event...
this.mylistbox.SelectionChanged += new SelectionChangedEventHandler(mylistbox_SelectionChanged);
this.mylistbox.Hold += new EventHandler<GestureEventArgs>(mylistbox_Hold);
}
void mylistbox_Hold(object sender, GestureEventArgs e)
{
if (this.currentItem == null) { return; }
this.currentItem.F_Color = "Yellow";
}
// respond to the SelectionChanged event...
void mylistbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.currentItem = ((ListBox)sender).SelectedItem as TB;;
}
}
public class TB
{
public string F_Name { get; set; }
public string F_Color { get; set; }
}
}