Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all I’m trying to load a combobox from a static list of a class US_State I made. The list gets populated in my WPF window with all the correct data but my combobox loads if I put ComboboxState.ItemsSource = Get_States; in my window code behind but I thought I could load the combobox by just binding. Here is my code I have

C#
public partial class ProfileScreen : Window
    {	//List populates with correct data
	public List<US_State> Get_States = StatesArray.Get_States;
     }
//xaml
<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"/>


My code in Us_State uses Inotify interface and static class just creates a List<us_state> and loads it.

What I have tried:

databinding dont work but this works in codebehind
Posted
Updated 20-Nov-18 2:48am

XAML
<ComboBox x:Name="ComboboxState" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Width="120" HorizontalAlignment="Left" ToolTip="Enter your state."
                          ItemsSource="{Binding Get_States} SelectedItem="{Binding SimpleStringProperty, Mode=TwoWay}" , UpdateSourceTrigger=PropertyChanged}"
                          DisplayMemberPath="Name" SelectedValuePath="Abbreviation"/>


Try this and see if it will work. Let me know if did not.
 
Share this answer
 
public List<US_State> Get_States = StatesArray.Get_States;

WPF data binding only works with properties. You've declared a field, which won't work.

If you check the output window in Visual Studio, you'll see a data binding error telling you that the property doesn't exist.

Try changing your field to a property:
public List<US_State> Get_States => StatesArray.Get_States;
 
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