Dear All
I would like to ask for is listbox.selectedItems only contains items that have been displayed on screen?
In details,
I modified listbox with my own template binding Text to code and IsSelected to isSelected in my Class Material.
I create a list of Material and convert to a listViewCollection and set to itemsource of the listbox.
While running the program , the isSelected field in the list is changed by code and I could also use listbox.selecteditems to get the selected list if the items are display on screen in the listbox.
However, if the listbox is not big enough to show all items, that is a scroll bar exists but never scrolled down, the item list get from listbox.selecteditems retrived is not complete, only those items shown on screen plus two to three items just below the visible list could be got.
If I scrolled down and scrolled back to top, the selecteditems list are correct.
Any comments? Thanks.
My code,
<ListBox Name="listBox_Material" Margin="3" Grid.Row="2" SelectionMode="Extended" Style="{StaticResource CustomListBox}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding code}" FontWeight="Bold" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding isSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Style x:Key="CustomListBox" TargetType="ListBox">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#2600A51D"/>
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#2600A51D" />
</Style.Resources>
</Style>
List<Material> lm = new List<Material>();
Material m = new Material();
m.code = "8B100";
m.cost = 125.00;
m.isSelected = false;
lm.Add(m);
ListCollectionView listCollectionView_Material = new ListCollectionView(lm); // Used for sorting
listBox_Material.ItemsSource = listCollectionView_Material;
Best Regards
swivan