user421719
user421719

Reputation: 237

WPF Listbox multi selection

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.

The style on XAML part

    <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.

Upvotes: 1

Views: 6014

Answers (1)

nvoigt
nvoigt

Reputation: 77364

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

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

Upvotes: 1

Related Questions