Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello I tried doing what you said by doing comment and looking at Microsoft. I made a class State with 3 properties (Name, abbreviations, and List<state> using INotifyChange in the properties. Then I made a static class with a List of state and in the constructor adding all 50 states to the List and a method to return the list to an array. Im not sure what else I need to do to bind my list to the combobox I feel that I am very close.
C#
public class US_State : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged();
            }
        }
        private string _abbreviations;
        public string Abbreviations
        {
            get => _abbreviations;
            set
            {
                _abbreviations = value;
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            //if(PropertyChanged!=null)PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private List<US_State> _allusstates;
        public List<US_State> AllUSStates
        {
            get => _allusstates;
            set
            {
                _allusstates = value;
                NotifyPropertyChanged();
            }
        }

        public US_State()
        {
            Name = null;
            Abbreviations = null;
        }
        public US_State(string ab, string name)
        {
            Name = name;
            Abbreviations = ab;
        }
        public override string ToString()
        {
            return String.Format("{0} - {1}", Abbreviations, Name);
        }
    }
    //Static class of all 50 US_state's
    public static class StatesArray
    {
        public static List<US_State> states;
        
        static StatesArray()
        {
            states = new List<US_State>(50);
            states.Add(new US_State("AL", "Alabama"));
            states.Add(new US_State("AK", "Alaska"));
//and the rest of the 50 states added just like this way above.        }
        public static US_State[] AllFiftyStates()
        {
            return states.ToArray();
        }
    }
Code in WPF Window
    public partial class ProfileScreen : Window
    {
private List<US_State> Get_States = StatesArray.states;
xaml code
<ComboBox x:Name="ComboboxState" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Width="120" HorizontalAlignment="Left" ToolTip="Enter your state."
                          ItemsSource="{Binding Get_States, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
                          DisplayMemberPath="Name" SelectedValuePath="Abbreviation"/>


What I have tried:

data table loading but wanting to use this interface way instead
Posted
Updated 14-Nov-18 17:19pm
Comments
Graeme_Grant 14-Nov-18 20:23pm    
What does the debug output tell you?
M.Kamran Asim 14-Nov-18 23:23pm    
Problem in your code is Get_States is private variable. You need to create public property. Binding only happens with public property. For complete solution see my posted Solution
Graeme_Grant 15-Nov-18 1:42am    
public List<US_State> AllUSStates {...} ... not the problem... ItemsSource="{Binding Get_States... should be ItemsSource="{Binding AllUSStates... ... the debugger output has the answer, that is why I asked...
M.Kamran Asim 15-Nov-18 4:00am    
AllUSStates Property is in wrong place. Why you are using US_State class for multi purpose. US_State act only as a Model, which is signature how you keep data.
while AllUSStates holds data of States. This property is supposed to be part of ViewModel, but in your case it is supposed to be part of windows.cs.

So Move this property to mainwindow, and keep US_State simple. This class will only hold attributes (Public properties).

1 solution

Hi
1. Your Us_State class is fine. Found No issue. Why you created AllUSStates public property in Us_State class. its not required.
2. Your static class StatesArray is Fine, but AllFiftyStates method is not required, You can declare states as public property, with public getter and private setter. So on constructor just fill states property, while publicly you can access it. Also you are adding additional cost by converting List<us_states> to array of us_states.
Note: this is suggestion.

3. Now finally once you have done, create public property in your mainWindow.xaml.cs. and implement INoityPropertyChanged.
private List<US_State> get_States;
public List<US_States> Get_States{
get=>get_States;
set{
      get_States = value;
      RaisePropertyChange();
   }
}


4. Now On window load event fill Get_States from source like Get_States= StatesArray.states;

5. Finally Find Get_States to Combobox. but remember, Combobox will look for datacontext of itesself, and then search throgh to its ancestor. But here as Your Get_States are availble in Windows.xaml.cs so you will use relative source like

HTML
<ComboBox x:Name="ComboboxState" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Width="120" HorizontalAlignment="Left" ToolTip="Enter your state."
                          ItemsSource="{Binding Get_States, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
                          DisplayMemberPath="Name" SelectedValuePath="Abbreviation"/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900