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

}

編集: これは、「ホールド」イベントが発生したときに選択されたアイテムがnullになるという事実に合わせて調整された、上記のコードの改訂です:

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 に答える