WPF 列表框多选

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19557677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:56:27  来源:igfitidea点击:

WPF Listbox multi selection

c#wpfxamlvisual-studio-2012listbox

提问by user421719

I am using wpf listbox (VS 2012 , C#) in my application.I am having trouble with multi-selection, the scenario is the listbox have buttons on listbox row I am able to open new form using this button as well update the value on listbox for this row but if I have selected multiple rows on the listbox the higlighted rows information is getting lost.For eg: if listbox is having 5 rows if I have selected row 2,3,4 if I click row3's button to open the pop up window and do update operation.The highlighted information is getting lost on close of popup.I am able to get the information in code behind of selected rows index on button's click event.How to apply this selected rows event in code behind to be reflected on UI.

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

The style on XAML part

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>

Button Click Event code

按钮点击事件代码

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

    }

Any Help would be appreciated.

任何帮助,将不胜感激。

采纳答案by nvoigt

You got your selected items that way, you need to set them that way as well:

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

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