我对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”,然后选择第五行,双击,将先前选择的条目的状态从“ false”更改为“ true”,完全忽略选定的行。然后,我双击第二行,它的状态从“ true”更改为“ false”,并且故事不断重复。
我检查了调试器,每第二次双击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
答案 0 :(得分: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;
}
}