简体   繁体   中英

WPF Combobox selection disappears when the an item is deleted in the list bound to combobox

I have a combo box rigged as

<ComboBox x:Name="HeadComboBox" ItemsSource="{Binding DataContext.HeadList, RelativeSource={RelativeSource FindAncestor,AncestorType= {x:Type views:FixedAssetBaseWholeUC}}}" Margin="195,78,86,0" VerticalAlignment="Top" SelectedItem="{Binding HeadItem}" DisplayMemberPath="Name" />

The datacontext.HeadList will point to:

public List<FixedAssetHeadItem> HeadList
    {
        get
        {
            return _headList;
        }
        set
        {
            if (_headList != value)
            {
                _headList = value;
                RaisePropertyChanged("HeadList");
            }
        }
    }

I disable the UserControl in which the combobox rests and load another control to edit the items in the headlist by

            DeleteFromHeadList(1);

            FixedAssetBaseWholeViewModel fbwvm = (FixedAssetBaseWholeViewModel)Fabwuc.DataContext;
            fbwvm.HeadList = HeadList;

When the edit is complete the re enable the usercontrol only to find the selection disappers.

Debug shows http://postimg.org/image/hdz4h4px3/

How should I deal with this?

You should not bind to List ( can cause memory leak ), but bind to ObservableCollection<> instead. In this way, your ComboBox should update appropriately. Also your HeadItem should be INPC property - in setter (private or public, depends on your code) should be raising of property changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM