Baskar
Baskar

Reputation: 9

Issue with Combo box Item Template

Please find the below code.

public enum SortByType
    {
        [Display(Name = "Y Value")]
        Y_Value,
        [Display(Name = "X Value")]
        X_Value,
        [Display(Name = "Z Value")]
        Z_Value,
    }
    public class ViewModel : INotifyPropertyChanged
    {
        ObservableCollection<SortByType> sortList;
        SortByType selectedType;
        public ViewModel()
        {
            SortList = new ObservableCollection<SortByType>(Enum.GetValues(typeof(SortByType)).OfType<SortByType>().ToList());
        }

        public ObservableCollection<SortByType> SortList
        {
            get
            {
                return sortList;
            }

            set
            {
                sortList = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SortList)));
            }
        }

        public SortByType SelectedType
        {
            get
            {
                return selectedType;
            }

            set
            {
                selectedType = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedType)));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }



public partial class MainWindow : Window
    {
        public MainWindow()
        {
            DataContext = new ViewModel();
            (DataContext as ViewModel).SelectedType = SortByType.Y_Value;
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            (DataContext as ViewModel).SortList.Clear();
            (DataContext as ViewModel).SortList = new ObservableCollection<SortByType>(Enum.GetValues(typeof(SortByType)).OfType<SortByType>().ToList());
        }
    }




<Grid>
        <StackPanel VerticalAlignment="Center">
            <ComboBox ItemsSource="{Binding SortList}" 
                      SelectedItem="{Binding SelectedType}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

            <Button Height="25" Width="150" Content="Details" Click="Button_Click"/>
        </StackPanel>
    </Grid>

Just, Clear the collection and re-initialize the combo box item source on button click event. On this time I have noticed that the Data error shown on the combo box. Can anyone explain what is the exact problem whether it may be a memory issue or some thing i have missed while implement the Item Template

Upvotes: 0

Views: 38

Answers (1)

mm8
mm8

Reputation: 169150

The value of SelectedType must be present in the source collection. It's not when you assign the source property to a new list of new values, unless you set explicitly set it to any of these new values:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var viewModel = DataContext as Win32ViewModel;
    viewModel.SortList = new ObservableCollection<SortByType>(Enum.GetValues(typeof(SortByType)).OfType<SortByType>().ToList());
    viewModel.SelectedType = viewModel.SortList.FirstOrDefault(x => x == viewModel.SelectedType);
}

Upvotes: 1

Related Questions