简体   繁体   中英

How to Determine Whether an Item from a List has been Selected Programmatically

I actually have two ListBoxes, and the user has the option of selecting an item from either ListBox. I'd like to have a way to determine whether an item has already been chosen from either list if a user tries to select it again. I do not want to indicate this by marking some sort of visual on the device's screen, because the item from either ListBox is already highlighted when selected. I just want to perform a check when the user selects an item from either ListBox to determine if an item has been selected, and if so display a message and not allow that item to be reselected.

XAML

<ListBox x:Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{Binding}" Margin="{Binding}"
                     toolkit:TiltEffect.IsTiltEnabled="True" SelectionChanged="ListBox_SelectionChanged" />

        <ListBox x:Name="ListBoxEffects1" SelectionMode="Single" ItemsSource="{Binding}" Margin="{Binding}"
                     toolkit:TiltEffect.IsTiltEnabled="True" SelectionChanged="ListBox_SelectionChanged" />

How do I keep track of what has been selected and what hasn't? To note, ListBoxEffects has 20 items and ListBoxEffects1 has 10 items, so there are 30 items total.

Since the items in the list are less, you can put the selected items in a collection and can validate through this collection while adding new items to it. If the item already exists you can display a message.

you can bind the SelectedItem, then you 'll get which item is selected in the code and you can show further messages like it is already been selected.

<ListBox x:Name="ListBoxEffects" SelectedItem="{Binding Model.SelectedList1Item,Mode=TwoWay}" SelectionMode="Single" ItemsSource="{Binding}" Margin="{Binding}"
                 toolkit:TiltEffect.IsTiltEnabled="True" SelectionChanged="ListBox_SelectionChanged" />

    <ListBox x:Name="ListBoxEffects1" SelectedItem="{Binding Model.SelectedList2Item,Mode=TwoWay}" SelectionMode="Single" ItemsSource="{Binding}" Margin="{Binding}"
                 toolkit:TiltEffect.IsTiltEnabled="True" SelectionChanged="ListBox_SelectionChanged" />

在您的代码中,您已经有SelectionChanged事件。您将使用SelectedValue属性获取selectedvalue。然后在SelectionChanged事件内部,您可以检查selectedvalue并使用它来做任何您想做的事情。

My requirement was slightly different but this logic kinda worked for me.

in xaml make stackpanel inside both the the listbox datatemplate

    <datatemplate>
    <stackpanel tap="stk_Tap">
<!--list item template, whatever it is-->
    </Stackpanel>
    </Datatemplate>

now in cs

just make two global int variables

int previousselectedindexList1=-1;

int previousselectedindexList2=-1;

now for tap event

 stk_Tap
    {
      if (previousselectedindexList1!= lbLocationHistory.SelectedIndex)
      {
        previousselectedindexList1= lbLocationHistory.SelectedIndex;
      }
      else
      {
        //your message here
        previousselectedindexList1= -1;
       }
    }

make same tap event for the stackpanel of other listbox

Hope something like this helps.

    private void ListBox_SelectionChanged( object sender, SelectionChangedEventArgs e)
    {
        String lstbox = (sender as TextBox).Name;
        switch(lstbox)
        {

            case "ListBoxEffects":
                    if(ListBoxEffects.SelectedItem == ListBoxEffects1.SelectedItem)
                    {
                        MessageBox.Show("Item already selected");
                    }
                    else
                     //ur code
                break;

           case "ListBoxEffects1":
                if(ListBoxEffects.SelectedItem == ListBoxEffects1.SelectedItem)
                {
                    MessageBox.Show("Item already selected");
                }
                else
                     //ur code
                break;
        }

    }

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