I meet the strange phenomenon. If I did a lot of Web Search, I can't get a solution. I use WPF, VS2010 engine. The GUI mainly consists of four ListBoxes.
First ListBox is aggregation of Mart Branches, Second ListBox is aggregation of goods as fruit, meat, vegetable ans so on that a Mart sells. Third ListBox is a kind of Mart goods. (apple, peach, peer, melon...) 4th ListBox is aggregation of a kind of selected fruit, for example, if Apple is selected, cortland, crabapple, sansa, gala and so on.
On starting Program, All Mart Branch is displayed on 1'st ListBox, and if I select one branch, Goods list that is sold at Mart is displayed on 2;nd listbox.
In the same way, a sub-kind of selected item is displayed on 3'rd ListBox and 4'th ListBox.
1'st, 2'nd, 4'th ListBox do good, But 3'rd ListBox have error. I think 2'nd and 3'rd have same structure.
3'rd ListBox cannot update selecteditem change. Regardless of SelectionMode (Single, Multiple, Extend), SelectedItems of 3'rd ListBox have all items I have selected. Furthermore, 3'rd ListBox.SelectedItems contains duplicated item.
But, SelectionChanged event firing is good. Only, SelectedItems or SelectedItem is problem.
Currently, to make a this function, I use detour way. After fire SelectionChanged, I catch AddedItems of SelelctionChangedEventArgs. So, I use AddedItems instead of SelectdItem like SelectionMode = "Single"
I tried many suggestion, VirtualizingStackPanel.IsVirtualizing="False", IsSynchronizedWithCurrentItem="True", But I cannot find solution. I'm sorry I cannot serve all behindcode and ~ xaml. Actually, This application is very big. So, I can't do that.
And, I'm sorry my English ability is poor.
Branches
<StackPanel Orientation="Vertical" Grid.Column="0">
<Label HorizontalAlignment="Center">Goods</Label>
<ListBox Name="lbLoadedGoods" Height="120" Margin="2" SelectionMode="Single" SelectionChanged="lbLoadedGoods_SelectionChanged"></ListBox>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="1" >
<Label HorizontalAlignment="Center">ITEM</Label>
<!-- ListBox Double Click Event Setter -->
<ListBox Name="lbLoadedItems" Height="120" Margin="2" SelectionMode="Single" SelectionChanged="lbLoadedItems_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem }}">
<EventSetter Event="MouseDoubleClick" Handler="lbLoadedItems_MouseDoubleClick"></EventSetter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</StackPanel>
</Grid>
</GroupBox>
<Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center">SubItem</Label>
<ListBox Name="lbSelectedSubItemData" Height="80" Grid.Column="0" Grid.Row="1" Margin="4">
<!-- ListBox Double Click Event Setter -->
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="MouseDoubleClick" Handler="lbSelectedSubItemData_MouseDoubleClick"></EventSetter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</StackPanel>
// public ObservableCollection<string> BranchList { get; private set; }
// public ObservableCollection<Goods> GoodList { get; private set; }
// public ObservableCollection<Items> ItemList { get; private set; }
// private ObservableCollection<string> m_usbitemlist = new ObservableCollection<string>();
// public ObservableCollection<string> SubItemList { get { return m_usbitemlist; } private set { m_usbitemlist = value; } }
private void BindFabFileList()
{
lbBranches.ItemsSource = BranchList;
lbLoadedGoods.ItemsSource = GoodList;
lbLoadedItems.ItemsSource = ItemList;
}
private void lbLoadedGoods_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ItemList = new ObservableCollection<Items>();
// ItemList Add. From Selected Goods
}
You're setting the reference to the object ItemList to a new instance, rather than updating the items on the list that WPF is already hooked up to. WPF automatically hooks up to the CollectionChanged event of ObservableCollection, but it does not know that you have changed the reference in the code behind. The UI is still connected to the old object in memory.
Instead of:
ItemList = new ObservableCollection<Items>();
ItemList.Add(...);
Do this:
ItemList.Clear();
ItemList.Add(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With