简体   繁体   中英

How to reset ListBox SelectedItem to it's orginal state. WP7

I have a WP7 ListBox with 2 items, "Line 1" and "Line 2". When I select an item, it will change the selected items state to selected and the text will be red.

After the selection I display a MessageBox, then select OK, and return to the ListBox where the item is still selected (red).

How do I reset the selected item to it's original state?

TIA, Trey

This is the code I'm using. It is a bit of a hack but kinda works.

 private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            if (listBox1.SelectedItem.ToString() == "Line 1")
            {
                MessageBox.Show("Found");
                ResetListBox();
            }
            else
            {
                MessageBox.Show("Not Found");
                ResetListBox();
            }
        }
        catch { }
    }

    private void ResetListBox()
    {
        listBox1.Items.Clear();
        listBox1.Items.Add("Line 1");
        listBox1.Items.Add("Line 2");
    }

store the previous selected item. After showing the messagebox, reset the selected item manually on the listbox

If you're using MVVM, then the ViewModel could take control of this Logic, clearing the selection after the user clicks Ok on the Messagebox. The code might look something like

public void Show()
{
     MessageBox.Show();
     SelectedCustomer = _previouslySelectedCustomer;
}

The property will Raise property changes and your list's SelectedItem property bound to it, that way the changes will get reflected in the UI.

If you're not using MVVM, then it will look something similar except that you would have to set it in Codebehind.

Hope this helps

Miguel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM