0

我有一个ListPicker (items=Week1,Week2)允许我在另一个列表中的 2 个列表之间切换ListBox

< toolkit:ListPicker x:Name="LP1"
                     Margin="12,8,12,0"
                     Grid.Row="1" 
                     VerticalAlignment="Top"
                     SelectedIndex="1"
                     SelectionChanged="WeekSelectionChanged"
                     ItemsSource="{Binding WeekSelection}" />

WeekSelectionChanged方法的逻辑ListPicker很简单:

if(this.LP1.SelectedIndex == 0) 
    this.ListBox1.ItemsSource = Week1; 
else if (this.LP1.SelectedIndex == 1) 
    this.ListBox1.ItemsSource = Week2; 

列表是这样组成的(重复第 2 周列表):

ObservableCollection<TB> Week1 = new ObservableCollection<TB>(); 
Week1.Add(new TB() { F_Name = "Day 1", F_Color = "Yellow" }); 
Week1.Add(new TB() { F_Name = "Day 2", F_Color = "Yellow" }); 
this.ListBox1.ItemsSource = Week1;

TB类是:

public class TB 
{
    public string F_Name { get; set; }
    public string F_Color { get; set; }     
}

当我“长按”一个列表项时,前景色变为灰色。但是当我在列表之间切换时,更改不会“保存”,而是显示原始的黄色。F_Color我想通过更改所选特定列表项的值来保留这些更改。

如何访问底层TB类及其属性?

4

1 回答 1

0

您必须在代码隐藏中或通过 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; }
}  

}

于 2012-09-27T22:51:01.010 回答