【问题标题】:How to Determine Whether an Item from a List has been Selected Programmatically如何确定是否已以编程方式选择列表中的项目
【发布时间】:2013-12-13 04:42:26
【问题描述】:

我实际上有两个 ListBox,用户可以选择从任一 ListBox 中选择一个项目。如果用户尝试再次选择它,我想有一种方法来确定是否已经从任一列表中选择了一个项目。我不想通过在设备屏幕上标记某种视觉来表示这一点,因为任一 ListBox 中的项目在选择时已经突出显示。我只想在用户从任一 ListBox 中选择一个项目时进行检查,以确定是否已选择某个项目,如果是,则显示一条消息并且不允许重新选择该项目。

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" />

如何跟踪已选择和未选择的内容?需要注意的是,ListBoxEffects 有 20 项,ListBoxEffects1 有 10 项,所以总共有 30 项。

【问题讨论】:

  • 为什么不直接使用您已经实现的 SelectionChanged 事件呢?

标签: c# xaml windows-phone-7 windows-phone-8


【解决方案1】:

由于列表中的项目较少,您可以将选定的项目放在一个集合中,并可以在向其中添加新项目时通过该集合进行验证。如果该项目已经存在,您可以显示一条消息。

【讨论】:

    【解决方案2】:

    您可以绑定 SelectedItem,然后您将获得在代码中选择了哪个项目,并且您可以显示更多消息,就像它已被选择一样。

    <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" />
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        我的要求略有不同,但这种逻辑对我来说有点用。

        在 xaml 中在列表框数据模板中创建 stackpanel

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

        现在在cs中

        只需创建两个全局 int 变量

        int previousselectedindexList1=-1;
        
        int previousselectedindexList2=-1;
        

        现在点击事件

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

        为其他列表框的堆栈面板制作相同的点击事件

        希望这样的事情有所帮助。

        【讨论】:

          【解决方案5】:
              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;
                  }
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-16
            • 2013-03-30
            • 1970-01-01
            • 1970-01-01
            • 2021-03-06
            相关资源
            最近更新 更多