Databinding 2 comboboxes Issue

喜欢而已 提交于 2019-12-04 19:40:31

Don't bind to the Text property in your comboboxes, try replacing:

 <ComboBox Text="{Binding CurrentActivity}"                                       
                                SelectedValuePath="ActivityID"                                        
                                DisplayMemberPath="ActivityName"
                                ItemsSource="{Binding Source={StaticResource ActivitiesDataProvider}}"/>

with:

<ComboBox ItemsSource="{Binding Source={StaticResource ActivitiesDataProvider}}"                                      
                                SelectedValuePath="ActivityID"                                        
                                DisplayMemberPath="ActivityName"
                                SelectedValue="{Binding CurrentActivity}"
                                IsSynchronizedWithCurrentItem="True"/>

Same idea for the sub activities combobox. In general, for comboboxes use the SelectedItem and SelectedIndex properties. Also use IsSynchronizedWithCurrentItem (depending on the type of ItemsSource.

By the way, you don't need to bind to the ID if you don't want to, you can bind directly to the whole object by using:

 SelectedValue="{Binding SelectedItem}"

which will bind the value of the combobox to the selected item of the list, just keep it in synch with your VM using IsSynchronizedWithCurrentItem="True" (this applies if your VM is managing the list and knows which the selected item is).

Hope this helps you! Regards

I resolved my issue. When An Activity is Changed, the related Sub-Activities should be notified of the change. But when the Sub-Activity is changed there is no need to notify the Activity.

I just commented OnPropertyChanged() call in CurrentSubActivity property. Here is the code

private int currentSubActivity;

    public int CurrentSubActivity
    {
        get { return currentSubActivity; }
        set
        {
            //OnPropertyChanged("CurrentSubActivity");
            this.currentSubActivity = value;

        }
    }

I hope this helps someone out there too :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!