C# WPF列表框多选

C# WPF列表框多选,c#,wpf,xaml,visual-studio-2012,listbox,C#,Wpf,Xaml,Visual Studio 2012,Listbox,我在我的应用程序中使用wpf列表框(VS 2012,C#)。我在多选方面遇到问题,场景是listbox在listbox行上有按钮我可以使用此按钮打开新表单,也可以更新此行在listbox上的值,但是如果我在listbox上选择了多行,高亮显示的行信息将丢失。例如:如果listbox有5行,如果我选择了行2、3、4,如果我单击行3的按钮打开弹出窗口并执行更新操作。在关闭弹出窗口时,突出显示的信息丢失。我能够在按钮的单击事件上获取所选行的代码隐藏索引中的信息。如何在代码隐藏中应用此所选行事件以反映在

我在我的应用程序中使用wpf列表框(VS 2012,C#)。我在多选方面遇到问题,场景是listbox在listbox行上有按钮我可以使用此按钮打开新表单,也可以更新此行在listbox上的值,但是如果我在listbox上选择了多行,高亮显示的行信息将丢失。例如:如果listbox有5行,如果我选择了行2、3、4,如果我单击行3的按钮打开弹出窗口并执行更新操作。在关闭弹出窗口时,突出显示的信息丢失。我能够在按钮的单击事件上获取所选行的代码隐藏索引中的信息。如何在代码隐藏中应用此所选行事件以反映在UI上

XAML部件上的样式

    <Style x:Key="AlternatingListViewItemStyle" TargetType="{x:Type ListBoxItem}">
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="White"/>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="DarkGray"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="True"/>
                            <Condition Property="ItemsControl.AlternationIndex" Value="0"/>
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter Property="Foreground" Value="LightBlue"/>
                            <Setter Property="Background" Value="LightBlue"/>
                        </MultiTrigger.Setters>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="True"/>
                            <Condition Property="ItemsControl.AlternationIndex"
                              Value="1"/>
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter Property="Foreground" Value="LightBlue"/>
                            <Setter Property="Background" Value="LightBlue"/>
                        </MultiTrigger.Setters>
                    </MultiTrigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"  Color="LightBlue"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightBlue"/>
                </Style.Resources>
                <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
            </Style>

<ListBox ItemsSource="{Binding Data}" ItemContainerStyle="{StaticResource AlternatingListViewItemStyle}" AlternationCount="2"   SelectionMode="Multiple" >
.
.
.
.
</Listbox>

.
.
.
.
按钮单击事件代码

 List<int> selectedItemIndexes = (from object o in listBox.SelectedItems select listBox.Items.IndexOf(o)).ToList();


    listBox.UnselectAll();

    foreach (int rowIndex in selectedItemIndexes)
    {

    // listBox.SelectedIndex = rowIndex;  // Tried this as well
     listBox.SelectedItem = listBox.Items.GetItemAt(rowIndex);

    }
List selecteditemindex=(从listBox.SelectedItems中的对象o选择listBox.Items.IndexOf(o)).ToList();
listBox.UnselectAll();
foreach(selecteditemindex中的int行索引)
{
//listBox.SelectedIndex=rowIndex;//也尝试了此操作
listBox.SelectedItem=listBox.Items.GetItemAt(rowIndex);
}

任何帮助都将不胜感激。

您以这种方式获得了所选项目,您还需要将它们设置为这种方式:

listBox.SelectedItems.Add( listBox.Items.GetItemAt(rowIndex) );

您以这种方式获取所选项目,还需要以这种方式进行设置:

listBox.SelectedItems.Add( listBox.Items.GetItemAt(rowIndex) );

如果要选择它们,首先取消选择它们的是谁?只是为了确保除了存储的SelectedItemIndex之外,没有其他行高亮显示。如果要选择它们,首先取消选择它们的是谁?只是为了确保除了存储的SelectedItemIndex之外,没有其他行高亮显示。