我在我的應用程序中使用wpf列表框(VS 2012,C#)。我有多選的麻煩,場景是列表框中有列表框行上的按鈕我可以使用此打開新窗體按鈕以及更新此行的列表框上的值,但如果我已經選擇列表框上的多行,那麼higlighted行信息正在lost.For例如:如果列表框有5行,如果我已經選擇行2,3,4如果我點擊row3的按鈕打開彈出窗口並執行更新操作。高亮顯示的信息在popup關閉時丟失。我可以在代碼後面的代碼中獲取所選行的索引按鈕的單擊事件中的信息。如何應用此選定行在代碼後面的事件將被反映在用戶界面上。WPF列表框多選
在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>
按鈕的Click事件代碼
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);
}
任何幫助,將不勝感激。
誰讓你不選擇他們擺在首位,如果你希望他們被選中? – nvoigt
只是爲了確保除存儲的selectedItemIndexes之外沒有突出顯示其他行。 – user421719