0
votes

I am trying to change the selected item of a combo box based on a change in another combo box. The situation is complicated by the fact that both combo boxes appear in a list of item templates.

The XAML is as follows:

<ListBox ItemsSource="{Binding AncillaryExtendedPropertyViewModels}" ItemTemplateSelector="{StaticResource templateSelector}"/>

<DataTemplate x:Key="EnumDataTemplate"> <Grid Margin="4"
MinHeight="25"> <ComboBox SelectedItem="{Binding ExtendedPropertyEnum,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ItemsSource="{Binding ExtendedPropertyEnumList}"
DisplayMemberPath="Value"/> </Grid> </DataTemplate>

The data context of the view containing the XAML is set to AncillaryBaseViewModel. The following is a cut down version of AncillaryBaseViewModel.

public class AncillaryBaseViewModel : ComplexOrderItemViewModel, IDataErrorInfo
    {
        private ObservableCollection<ExtendedPropertyViewModel> _ancillaryExtendedPropertyViewModels;

public ObservableCollection<ExtendedPropertyViewModel> AncillaryExtendedPropertyViewModels
        {
            get { return _ancillaryExtendedPropertyViewModels; }
            set
            {
                _ancillaryExtendedPropertyViewModels = value;
                OnPropertyChanged("AncillaryExtendedPropertyViewModels");
            }
        }

and the ExtendedPropertyViewModel class....

    public class ExtendedPropertyViewModel : DataTemplateSelector
    {
        private ExtendedProperty _extendedProperty;
        public DataTemplate DefaultnDataTemplate { get; set; }
        public DataTemplate BooleanDataTemplate { get; set; }
        public DataTemplate EnumDataTemplate { get; set; }

        public ExtendedPropertyEnum ExtendedPropertyEnum
        {
        get
        { return ExtendedProperty.ExtendedPropertyEnum; }
        set
        {
            if (ExtendedProperty.ExtendedPropertyEnum != value)
            {
                _extendedProperty.ExtendedPropertyEnum = value;
                AncillaryBaseViewModel parent = RequestParent();
                if (parent != null)
                { 
parent.AncillaryExtendedPropertyViewModels[7].ExtendedPropertyEnum = 
ExtendedProperty.ExtendedPropertyEnum.DependentExtendedPropertyEnums[0];
                }
                parent.OrderItem.SetStockCode();
                PropertyChanged += parent.OnExtendedPropertyChanged;
                OnPropertyChanged();
            }
        }
    }

and the ExtendedProperty class....

public class ExtendedProperty : ViewModelBase
{
    private ExtendedPropertyEnum _extendedPropertyEnum;
    public int ExtendedPropertyID { get; set; }
    public int OrderItemTypeID { get; set; }
    public string DisplayName {get; set;}
    public ObservableCollection<ExtendedPropertyEnum> ExtendedPropertyEnumList { get; set; }
    public string Value { get; set; }

    public ExtendedPropertyEnum ExtendedPropertyEnum
    {
        get
        {
            return _extendedPropertyEnum;
        }
        set
        {
            _extendedPropertyEnum = value;
            OnPropertyChanged("ExtendedPropertyEnum");
        }
    }
}

To summarise, when I change the value of combo box A through the UI, this calls the ExtendedPropertyEnum setter within ExtendedPropertyViewModel, which changes the ExtendedPropertyEnum bound to another combo box B, which is in the same list. I would expect this to update the value displayed in combo box B accordingly, which it does not. As an aside, changing the value of combo box A does update a label that is not within a data template. The XAML for this label is....

<Label Content="{Binding StockCode}" MinWidth="100"/>

This is updated by the following code within AncillaryBaseViewModel....

public void OnExtendedPropertyChanged(object sender, EventArgs args)
{
    OnPropertyChanged("StockCode");
}

I thought I could change this to the following to force the combo boxes in the list to update.

public void OnExtendedPropertyChanged(object sender, EventArgs args)
{
    OnPropertyChanged("StockCode");
    OnPropertyChanged("AncillaryExtendedPropertyViewModels");
}

However, this did not work either.

Any help greatly appreciated. Roger.

1
Sorry folks, I had commented out the XAML for the DataTemplate. This shows the SelectedItem (ExtendedPropertyEnum) and ItemSource (ExtendedPropertyEnumList) of the combo box. I hope that makes the question a bit clearer.Roger Smith

1 Answers

0
votes

if I understand the question correctly then you are expecting a changed value within an observablecollection to be reflected within the UI. This will not happen. observablecollections raise notifyproperty events when the collection itself changes, not when values within them change. You'll either need to raise a notifyproperty event on the value changing or remove the item from the list and add it back with a new value.