【问题标题】:Listbox SelectionChanged firing when setting to -1 within function, wp7在函数 wp7 中设置为 -1 时,Listbox SelectionChanged 触发
【发布时间】:2011-10-06 19:39:45
【问题描述】:

我在 c# 中遇到了一个非常奇怪的问题,我只是想知道是什么原因造成的。我有我的理论,但不完全确定,只是想看看它是否可以复制。

wp7 silverlight 4 中的标准数据透视页。

<Pivot>
  <PivotItem>
     <Listbox Width="400" Height="500" x:Name="box" SelectionChanged="myhandle">

        <ListBoxItem x:Name="item1">
           <TextBlock Height="40" Width="200" Text="hi everyone!"/>
        </ListBoxItem>

        <ListBoxItem x:Name="item2">
           <TextBlock Height="40" Width="200" Text="No Wai"/>
        </ListBoxItem>

        <ListBoxItem x:Name="item3">
           <TextBlock Height="40" Width="200" Text="Ya Rly!"/>
        </ListBoxItem>

     </Listbox>
  </PivotItem>
</Pivot>

在我的 C# 中,我有以下内容:

  private void myhandle(object sender, SelectionChangedEventArgs args)
  {
    var selection ="";
    selection = (sender as Listbox).SelectedIndex.ToString();
    box.SelectedIndex = -1;
  }

问题是:每当我单击三个列表框项之一时,myhandle 代码会使选择等于正确的 SelectedIndex,但随后它会点击box.SelectedIndex =-1; 行,然后点击refires myhandle 函数。这样做时,选择现在是 -1。

我不知道为什么它会返回堆栈。这不应该是递归函数。

我的目标是选择该项目,然后将 SelectedIndex 恢复为 -1,以便该人能够在需要时再次选择该项目,而不是更改为另一个项目并返回。

当然有一个简单的解决方法,即抛出一个 switch 函数并检查它是否已经为 -1,但这并不能解决递归问题。

感谢您的宝贵时间。

【问题讨论】:

    标签: windows-phone-7 silverlight-4.0 listbox selectedindex


    【解决方案1】:

    每次更改选择时,SelectionChanged 事件都会触发。这包括清除选择,包括设置 SelectedIndex = -1,即使您已经在 SelectionChanged 处理程序中。

    你可以这样做:

    private bool inMyHandle = false;
    private void myhandle(object sender, SelectionChangedEventArgs args)
    {
        if (!this.isMyHandle) {
            this.isMyHandle = true;
            try {
                var selection ="";
                selection = (sender as Listbox).SelectedIndex.ToString();
                box.SelectedIndex = -1;
            }
            finally {
                this.isMyHandle = false;
            }
        }
    }
    

    【讨论】:

    • 感谢您的澄清!
    • 也感谢您提供示例代码。我改变了'private inMyHandle = false;'到“私人布尔 isMyHandle = false;”跟随代码的其余部分。
    • @ShaunDoty - 糟糕,已解决。谢谢!
    【解决方案2】:

    标准的 MS 样本已经在标准的列表框选定项事件中包含此内容。

    只需在事件处理程序代码中使用以下代码:

        private void ListBox_SelectionChanged(object sender,System.Windows.Controls.SelectionChangedEventArgs e)
    {
        // If selected index is -1 (no selection) do nothing
        if (ListBox.SelectedIndex == -1)
            return;
    
        //Do Something
    
        // Reset selected index to -1 (no selection)
        ListBox.SelectedIndex = -1;
    }
    

    不需要任何布尔处理程序,如果“-1”是当前索引,则该函数什么都没有。 这一切都是为了补偿标准列表框的运作方式。

    如果您使用 MVVM 并绑定到“Selecteditem”/“SelectedIndex”属性,您必须记住同样的事情。

    【讨论】:

      【解决方案3】:

      您也可以检查 args.AddedItems.Count:

      private void myhandle(object sender, SelectionChangedEventArgs args) 
      {
         if (args.AddedItems.Count > 0)
         {
             ....
             box.SelectedIndex = -1;
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-03
        • 1970-01-01
        • 2014-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        相关资源
        最近更新 更多