3
votes

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
    }
1
I think your problem is your data is not getting updated ..and also you are setting itemsSource of your ListBoxes to some list..so what your solution could be..set the itemsSource again when you are changing selected items or selected item..and also make your list to observablecollections..it helps you i think...for any query update below..loop
if I am understand you correctly this is Master Details Sceranio read about IsSynchronizedWithCurrentItem="True"makc
ItemsSource of my ListBox is ObservableCollection as you see above. "ItemList" is ObservableCollection. All the operation is good. Just "lbLoadedItems.SelectedItems" is not updating. All items that I select is accumulated just in "lbLoadedItems" I'll consider more your recommendation. Thanks for your interest.chaji99
All items that I select is accumulated just in "lbLoadedItems" : I rewrite "lbLoadedItems ==> lbLoadedItems.SelectedItems" The problem I have is just about not updating SelectedItems property of ListBox .chaji99

1 Answers

1
votes

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