I am able to populate List in the code behind but the values do not bind to the combobox in xaml

Here is the sample code

 public IEnumerable<string> _AllProperties;
 public IEnumerable<string> AllProperties
        {
            get { return _AllProperties; }
            set
            {
                if (_AllProperties == value) return;
                _AllProperties = value;

                OnPropertyChanged("AllProperties");
            }
        }     

 public MainWindow()
        {
            InitializeComponent();
            // Create a list  
            List<string> PropertyValues = new List<string>();
            // Add a range of items  
            string[] values = {  "BlackOilFluid",
            "Boundary",
            "Casing",
            "CheckValve",
            "Choke",
            "Completion",
            "CompletionConingPoint",
            "CompletionModel",
            "CompositionalFluid",
            "Compressor",
            "EngineKeywords",
            "ESP",
            "Expander",
            "FileBasedFluid",
            "Flowline",
            "FluidComponent",
            "GasLiftInjection",
            "GenericBooster",
            "GenericEquipment",
            "GravelPack",
            "HeatExchanger",
            "Injector",
            "IPRBackPressure",
            "IPRDarcy",
            "IPRFetkovitch",
            "IPRForchheimer",
            "IPRHorizontalPI",
            "IPRHydraulicFracture",
            "IPRJones",
            "IPRPIModel",
            "IPRPSSBabuOdeh",
            "IPRSSJoshi",
            "IPRVogel",
            "Junction",
            "Liner",
            "MeasurementPoint",
            "MFLFluid",
            "MultiphaseBooster",
            "MultiplierAdder",
            "NetworkSim",
            "NodalAnalysisOp",
            "OneSubseaBooster",
            "OpenHole",
            "Packer",
            "PCP",
            "PTProfileSim",
            "Pump",
            "PVTFluid",
            "RodPump",
            "SinglephaseSeparator",
            "Sink",
            "SlidingSleeve",
            "Source",
            "Study",
            "SubsurfaceSafetyValve",
            "ThreePhaseSeparator",
            "Tubing",
            "TubingPlug",
            "TwoPhaseSeparator",
            "WaterTempVelocitySurvey",
            "Well",
            "WetGasCompressor" };

            PropertyValues.AddRange(values);

            _AllProperties = PropertyValues;

        }

I expect the combo box to be populate with the above defined list of strings.

Here is the XAML code:

<ComboBox Grid.Column="2" x:Name="propvar" HorizontalAlignment="Center"  VerticalAlignment="Center" Height="30" Width="100" Margin="5,5,5,5"   IsTextSearchEnabled="True"  FontSize="11"
ItemsSource="{Binding AllProperties}" SelectionChanged="Var_SelectionChanged"
SelectedItem="{Binding Propselected, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />

I do not quite understand why the values are not displayed in the combobox dropdown.

1 Answers

0
Community On Best Solutions

Try this:

Xaml:

<Window x:Class="TestChipDeleting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="400" 
        Width="400">

    <StackPanel VerticalAlignment="Center"
                DataContext="{Binding}">

        <ComboBox ItemsSource="{Binding Path=Properties}"
                 IsTextSearchEnabled="True" 
                 FontSize="11"
                 Margin="8"
                 Height="30" 
                 Width="200"
                 HorizontalAlignment="Center"  
                 VerticalAlignment="Center" 
                 SelectionChanged="Var_SelectionChanged"
                 SelectedItem="{Binding Propselected, 
            NotifyOnValidationError=True,
            UpdateSourceTrigger=PropertyChanged,
            ValidatesOnDataErrors=True, 
            ValidatesOnExceptions=True}"/>

    </StackPanel>
</Window>

C#:

namespace Test
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }

        private void Var_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }


    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            BindItems();
        }

        private List<string> _properties;

        public List<string> Properties
        {
            get { return _properties; }
            set
            {
                if (value.Equals(_properties))
                    return;
                _properties = value;
                OnPropertyChanged(nameof(Properties));
            }
        }

        private void BindItems()
        {
            string[] items =
            {
                "BlackOilFluid",
                "Boundary",
                "Casing",
                "CheckValve",
                "Choke",
                "Completion",
                "CompletionConingPoint",
                "CompletionModel",
                "CompositionalFluid",
                "Compressor",
                "EngineKeywords",
                "ESP",
                "Expander",
                "FileBasedFluid",
                "Flowline",
                "FluidComponent",
                "GasLiftInjection",
                "GenericBooster",
                "GenericEquipment",
                "GravelPack",
                "HeatExchanger",
                "Injector",
                "IPRBackPressure",
                "IPRDarcy",
                "IPRFetkovitch",
                "IPRForchheimer",
                "IPRHorizontalPI",
                "IPRHydraulicFracture",
                "IPRJones",
                "IPRPIModel",
                "IPRPSSBabuOdeh",
                "IPRSSJoshi",
                "IPRVogel",
                "Junction",
                "Liner",
                "MeasurementPoint",
                "MFLFluid",
                "MultiphaseBooster",
                "MultiplierAdder",
                "NetworkSim",
                "NodalAnalysisOp",
                "OneSubseaBooster",
                "OpenHole",
                "Packer",
                "PCP",
                "PTProfileSim",
                "Pump",
                "PVTFluid",
                "RodPump",
                "SinglephaseSeparator",
                "Sink",
                "SlidingSleeve",
                "Source",
                "Study",
                "SubsurfaceSafetyValve",
                "ThreePhaseSeparator",
                "Tubing",
                "TubingPlug",
                "TwoPhaseSeparator",
                "WaterTempVelocitySurvey",
                "Well",
                "WetGasCompressor"
            };

            Properties = new List<string>(items);
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}