Listbox SelectionChanged 在函数 wp7 中设置为 -1 时触发

发布于 2024-12-08 16:49:22 字数 1336 浏览 0 评论 0原文

我在 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; 行,然后重新触发 myhandle 函数。这样做时,选择现在为-1。

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

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

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

谢谢你的时间。

I am running into a very odd problem in c# and I just wanted to know what is causing this. I have my theories, but not entirely sure and just want to see if it can be reproduced.

Standard Pivot Page in 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>

In my C#, I have the following:

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

Here is the problem: Whenever I click on one of the three listboxitems, the myhandle code makes selection equal to the proper SelectedIndex, but then it hits the box.SelectedIndex =-1; line and then refires the myhandle function. In doing so, selection is now -1.

I have no idea why it is going back up the stack. This shouldn't be a recursive function.

My goal is to select the item, but then have the SelectedIndex back to -1 so that the person is able to select the item once again if need be, instead of changing to another item and back.

Sure there is an easy fix of throwing a switch function and checking to see if it's -1 already, but that doesn't solve the problem of the recursion.

Thanks for the time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-12-15 16:49:22

每次更改选择时,都会触发 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;
        }
    }
}

Everytime the selection is changed, the SelectionChanged event will fire. This includes clearing the selection, which includes setting SelectedIndex = -1 and even if you are already in a SelectionChanged handler.

You can do something like this:

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;
        }
    }
}
飞烟轻若梦 2024-12-15 16:49:22

标准 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”属性,您必须记住同样的事情。

The standard MS samples already have this in a standard Listbox selected item event.

Simply use the following in the event handler code:

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

No need to have any boolean handlers, the function just nothing if "-1" is the current index.
All this is to compensate for the way that the standard listbox opperates.

If you use MVVM and bind to the "Selecteditem" / "SelectedIndex" properties you have to keep the same thing in mind.

感情废物 2024-12-15 16:49:22

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

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

You can check args.AddedItems.Count too:

private void myhandle(object sender, SelectionChangedEventArgs args) 
{
   if (args.AddedItems.Count > 0)
   {
       ....
       box.SelectedIndex = -1;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文