简体   繁体   中英

WPF ComboBox Refresh After Collection Update

I am using ComboBox in wpf as below and want to update ComboBox behind the seen if i update collection :-

     <xmlns:dataProvider="clr-namespace:DataProvider"
     <UserControl.Resources>
        <dataProvider:BackOfficeDataProvider x:Key="DataProvider"/>
     </UserControl.Resources>
    <ComboBox x:Name="groupGroupNameCombo" HorizontalAlignment="Left" Margin="368,123,0,0" VerticalAlignment="Top" Width="226" Height="31" SelectionChanged="groupGroupNameCombo_SelectionChanged"    DisplayMemberPath="GroupName" SelectedItem="{Binding ParentID, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding GroupParentList, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, Source={StaticResource DataProvider}}" IsSynchronizedWithCurrentItem="True">

                        </ComboBox>

    Class BackOfficeDataProvider {
    public static ObservableCollection<Categories> groupParentList = null;
    public virtual ObservableCollection<Categories> GroupParentList
            {
                get { return groupParentList ; }
                set
                {
                    groupParentList = value;
                    // Call OnPropertyChanged whenever the property is updated
                    OnPropertyChanged("GroupParentList");
                }
            }

    public void loadComboListData();
    {
    GroupParentList = (ObservableCollection<Categories>) //fetching data from database using NHibernate directly getting list ;
    }
}

my front end class which has refresh button :-

private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
          new BackOfficeDataProvider().loadComboListData();
        }

when application load that time i can see the item in combobox but when i click on Refresh button that time it load updated data from database but not updating combobox untill i use below code

groupGroupNameCombo.ItemsSource = null;
groupGroupNameCombo.ItemSource = GroupParentList ;

Its a manually thing i have to do always to refresh combobox , how can i make it automatic like if i update collection then it should update combobox at the same time and i don't need to use above workaround.

I think this may have something to do with breaking the coupling between the combobox and the ObservableCollection when doing this:

GroupParentList = //fetching data from database;

Try this instead:

var dbCategories = // Get data from DB
GroupParentList.Clear(); 
foreach (var item in dbData)
    GroupParentList.Add(item);

The point is to update the items in the collection, not the collection itself.

Also, try defining your collection like this, it should'nt have to be instantiated more than once (ie no setter):

public static ObservableCollection<Categories> groupParentList = null;
public virtual ObservableCollection<Categories> GroupParentList
{
    get 
    {
        if (groupParentList == null)
            groupParentList = new ObservableCollection<Categories>();
        return groupParentList; 
    }
}

Hogler is right, your approach of assigning a new ObservableCollection object to the binding property will break how binding works. For ObservableCollection to work, you will need to modify the items in the collection itself, ObservableCollection is responsible of publishing list changes to the binding target. When you assign a new collection to the binding target, the list will not get refresh unless you published PropertyChanged event again to register this new binding source.

In your later comment you did state that you only instantiate ObservableCollection once only, which is not obvious from your posted code. It appears to me that the reason why it doesn't work is because you assign a new collection to the "GroupParentList" each time you run "loadComboListData".

试试这个..一旦完成从groupParentList的数据库中获取数据,在Line下面添加,它将按以下方式工作:

GroupParentList = new ObservableCollection<Categories>(groupParentList )

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