【问题标题】:ListView's SelectedIndex wrong valueListView 的 SelectedIndex 值错误
【发布时间】:2019-01-13 23:28:32
【问题描述】:

ListView 的SelectedIndex 返回的值有问题。函数如下所示:

private void ChangeCableStatus_DoubleClick(object sender, RoutedEventArgs e)
{
    int index = tableOfCables.SelectedIndex;
    if (index == -1) // selected empty entry
    {
        return;
    }

    var selectedEntry = tableOfCables.SelectedItems[0] as Cable;
    var node1 = selectedEntry.Node1;
    var port1 = selectedEntry.Port1;
    var node2 = selectedEntry.Node2;
    var port2 = selectedEntry.Port2;

    cableCloud.Config.Cables.FirstOrDefault(cable =>
        cable.Node1.Equals(node1) && cable.Node2.Equals(node2) && cable.Port1.Equals(port1) &&
        cable.Port2.Equals(port2)).Working ^= true;

    tableOfCables.Items.Refresh();
}

所以,当我双击表格中的任何条目时,它应该将状态从“true”更改为“false”,反之亦然。

第一次双击后,它工作正常,并且所选索引中的状态变为“false”。但是,无论我单击哪个条目,它都会将先前选择的条目的状态从“false”更改为“true”。例如:我双击第一行,它将状态从“true”更改为“false”,然后我选择第5行,双击,它将先前选择的条目的状态从“false”更改为“true”,完全忽略选定的行。然后,我双击,比如说,第二行,它的状态从“真”变为“假”,故事重演。 我用调试器检查过,每第二次双击SelectedIndex 值不会改变。如何解决这个问题?

<ListView x:Name="tableOfCables" Margin="10,10,10,10" PreviewKeyDown="ChangeFontSizeKeyboard" PreviewMouseWheel="ChangeFontSizeMouse" IsSynchronizedWithCurrentItem="True" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled" AlternationCount="2">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Style.Triggers>
                        <Trigger Property="ItemsControl.AlternationIndex"  Value="0">
                            <Setter Property="Background" Value="White" />
                        </Trigger>
                        <Trigger Property="ItemsControl.AlternationIndex"  Value="1">
                            <Setter Property="Background" Value="#f8f8f8" />
                        </Trigger>
                    </Style.Triggers>
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <EventSetter Event="MouseDoubleClick" Handler="ChangeCableStatus_DoubleClick" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridView.ColumnHeaderContainerStyle>
                        <Style TargetType="{x:Type GridViewColumnHeader}">
                            <Setter Property="FontSize" Value="15"/>
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="FontWeight" Value="Bold"/>
                        </Style>
                    </GridView.ColumnHeaderContainerStyle>
                    <GridViewColumn  Header="Node1" Width="85" DisplayMemberBinding="{Binding Node1}"/>
                    <GridViewColumn Header="Port1" Width="85" DisplayMemberBinding="{Binding Port1}"/>
                    <GridViewColumn Header="Node2" Width="85" DisplayMemberBinding="{Binding Node2}"/>
                    <GridViewColumn Header="Port2" Width="85" DisplayMemberBinding="{Binding Port2}"/>
                    <GridViewColumn Header="Working" Width="100" DisplayMemberBinding="{Binding Working}"/>
                    <GridViewColumn Header="Max BW" Width="70" DisplayMemberBinding="{Binding MaxBandwidth}"/>
                    <GridViewColumn Header="Free BW" Width="70" DisplayMemberBinding="{Binding FreeBandwidth}"/>
                </GridView>
            </ListView.View>
        </ListView>

如果有人感兴趣,整个项目都在这里(启动参数是./Resources/cloud.cloudconfig):https://drive.google.com/file/d/11e84tqgTqXJ1hsYCQOTmA4prV0r9kQNW/view?usp=sharing

【问题讨论】:

  • 尝试处理GridViewMouseDoubleClick事件
  • @SantaXL 我调试了您的代码,并检查了引发双击事件时索引没有变化。我猜还有其他事件/方法导致控件的索引为 0。
  • 能详细说明一下ChangeFontSizeKeyboard是做什么的吗?
  • 我建议你看看 mvvm。绑定选中项,使用鼠标绑定双击。您可能遇到的一个问题是在 selectedindex 更改之前引发了事件。
  • @itaiy 当 ctrl 和 "-"/"+" 同时按下时,它会减小/增大字体大小

标签: c# wpf


【解决方案1】:

我检查了您的来源,这是因为您在 Cable 类中手动覆盖了哈希码,删除此块问题将得到解决:

    public override int GetHashCode()
    {
        unchecked
        {
            int hashCode = (Node1 != null ? Node1.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ Port1.GetHashCode();
            hashCode = (hashCode * 397) ^ (Node2 != null ? Node2.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ Port2.GetHashCode();
            hashCode = (hashCode * 397) ^ Working.GetHashCode();
            hashCode = (hashCode * 397) ^ MaxBandwidth;
            return hashCode;
        }
    }

【讨论】:

  • 感谢您的努力,但没有帮助。我添加了整个项目的链接(小于 300 LoC)
  • 源需要访问
  • 编辑:抱歉,我忘了设置文件的访问权限。现在应该可以下载了
  • 谢谢,我不知道这会影响这部分代码
  • 当您覆盖哈希码容器时无法从列表视图中找到真实项目,因为项目点手动更改
猜你喜欢
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
相关资源
最近更新 更多