listBox在WPF中遇到麻烦

时间:2013-08-19 13:00:15

标签: c# wpf listbox wpf-controls

我正在使用WPF和C#开发一个项目。在一个窗口上,我有大约7个listBox,它们包含相同数量的Items。这些项目实际上是相互关联的。它类似于网格。

lisBox1 | lisBox2 | lisBox3 | lisBox4 | lisBox5 | lisBox6 | lisBox7
  

每当更改第三个ListBox的SelectedIndex时,全部   其他列表框的SelectedIndex设置为等于   第三个listBox的SelectedIndex。这允许用户突出显示   用户实际正在阅读哪一行。

Screenshot from my machine

当我在运行Windows XP的客户端计算机上部署项目时,索引管理得不好!每个listBox的索引定期更改,我无法从所有列表框中获取所选项的索引,即每个listBox可能/可能没有不同的索引。

另外我注意到在客户端的机器上,列表框能够选择多个项目,我在运行VS10和Windows 8的开发机器中禁用并正常工作。

  

它与Windows XP有关吗?

请告诉我这些问题可能是什么原因?任何想要解决的想法将不胜感激。提前谢谢。

标记

<ListBox Height="auto" HorizontalAlignment="Left" Margin="30,201,0,0" Name="listBox1" VerticalAlignment="Top" Width="40" FontStyle="Italic" SelectionChanged="listBox1_SelectionChanged" />
<ListBox Height="auto" HorizontalAlignment="Left" Margin="75,201,0,0" Name="listBox2" VerticalAlignment="Top" Width="85" FontStyle="Italic" SelectionChanged="listBox2_SelectionChanged" />
<ListBox Height="auto" HorizontalAlignment="Left" Margin="163,201,0,0" Name="listBox3" VerticalAlignment="Top" Width="190" FontWeight="Bold" FontStyle="Italic" SelectionChanged="listBox3_SelectionChanged" KeyDown="listBox3_KeyDown" />
<ListBox Height="auto" HorizontalAlignment="Left" Margin="355,201,0,0" Name="listBox4" VerticalAlignment="Top" Width="90" FontStyle="Italic" SelectionChanged="listBox4_SelectionChanged" />
<ListBox Height="auto" HorizontalAlignment="Left" Margin="449,201,0,0" Name="listBox5" VerticalAlignment="Top" Width="55" FontStyle="Italic" SelectionChanged="listBox5_SelectionChanged" />
<ListBox Height="auto" HorizontalContentAlignment="Right" HorizontalAlignment="Left" Margin="505,201,0,0" Name="listBox6" VerticalAlignment="Top" Width="80" FontWeight="Bold" SelectionChanged="listBox6_SelectionChanged" />
<ListBox Height="auto" HorizontalContentAlignment="Right" HorizontalAlignment="Left" Margin="590,201,0,0" Name="listBox7" VerticalAlignment="Top" Width="80" FontWeight="Bold" SelectionChanged="listBox7_SelectionChanged" />
<ListBox Height="auto" HorizontalContentAlignment="Right" HorizontalAlignment="Left" Margin="675,201,0,0" Name="listBox8" VerticalAlignment="Top" Width="95" FontWeight="Bold" SelectionChanged="listBox8_SelectionChanged" />

代码:

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    listBox2.SelectedIndex = listBox1.SelectedIndex;
}

private void listBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    listBox3.SelectedIndex = listBox2.SelectedIndex;
}

private void listBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    listBox4.SelectedIndex = listBox3.SelectedIndex;
}

private void listBox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    listBox5.SelectedIndex = listBox4.SelectedIndex;
}

private void listBox5_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    listBox6.SelectedIndex = listBox5.SelectedIndex;
}

private void listBox6_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    listBox7.SelectedIndex = listBox6.SelectedIndex;
}

private void listBox7_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    listBox8.SelectedIndex = listBox7.SelectedIndex;
}
private void listBox8_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            listBox1.SelectedIndex = listBox8.SelectedIndex;
        }

How Indexes Should Be这就是我的电脑,完美!

How Indexes are on Windows XP这就是它在XP上的表现。

How multiselect is Ok on my machine 这就是我的电脑上多重选择关闭的方式,并且工作正常。

How multiselect malfunctioned on Windows XP这就是多选的开启方式,并且总是在Windows XP上选择listbox3中的第一项。

1 个答案:

答案 0 :(得分:1)

如果所有列表框都绑定到同一个集合,则可以使用IsSynchronizedWithCurrentItem 属性。如果你在所有列表框中将它设置为true,并将所有列表框绑定到同一个集合,则它们应该对齐。

对于多项选择,您尚未指定SelectionMode。尝试将其明确设置为Single。

此致 约尼。