Binding combobox to a simple array of strings

I have a simple class that provides state codes as follows:

public class StateProvider
{
    static string[] _codes = new string[]
    {
        "AL",
        "AK",
        ...
    };

    public string[] GetAll()
    {
        return _codes;
    }
}

My model class supporting the view looks something like this:

public class ProjectModel : ChangeNotifier
{
    StateProvider _states = new StateProvider();

    public ProjectModel()
    {
        Project = LoadProject();
    }

    ProjectEntity _project;
    public ProjectEntity Project
    {
        get { return _project; }

        set
        {
            _project = value;
            FirePropertyChanged("Project");
        }
    }

    public string[] States { get { return _states.GetAll(); } }
}

And my ComboBox XAML looks like this:

<ComboBox SelectedValue="{Binding Project.State, Mode=TwoWay}" SelectedValuePath="{Binding RelativeSource={RelativeSource Self}}" ItemsSource="{Binding States}" />

The binding works with the user interface on the object - if I select the state from the combo, the value will be transferred to the project object, and I can save it. However, if I turned off and rebooted, the value of the status code is not bound from the object to the user interface, and the combo does not display anything. Then, of course, subsequent storage returns the state value of the object.

, , ( ). State, Code FullName, SelectedValuePath DisplayMemberPath .

: , ProjectModel . , ProjectEntity . . , Entity, . TwoWay , combobox.

+3
3

, whodathought :

<ComboBox ItemsSource="{Binding States}" SelectedValue="{Binding Project.State, Mode=TwoWay}" />

, , XAML. SelectedValue ItemsSource , , , SelectedValue.

, .

+1

IPropertyNotifyChanged ProjectModel

public class ProjectModel : INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;

Project, , , 1-way-1-time.

public ProjectEntity Project
{
    get { return (ProjectEntity)GetValue(ProjectProperty); }
    set { SetValue(ProjectProperty, value); }
}

// Using a DependencyProperty as the backing store for Project.  
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty ProjectProperty =
    DependencyProperty.Register("Project",
                                typeof(ProjectEntity),
                                typeof(ProjectModel),
                                new PropertyMetadata(null,
                                    new PropertyChangedCallback(OnProjectChanged)));

static void OnProjectChanged(object sender, DependencyPropertyChangedEventArgs args)
{
    // If you need to handle changes
}
+1

Change the ProjectModel class to this:

public class ProjectModel : ChangeNotifier
{
    StateProvider _states = new StateProvider();

    public ProjectModel()
    {
        Project = LoadProject();

        States = new ObservableCollection<string>(_states.GetAll());
    }

    ProjectEntity _project;
    public ProjectEntity Project
    {
        get { return _project; }

        set
        {
            _project = value;
            FirePropertyChanged("Project");
        }
    }

    public ObservableCollection<string> States { get; set; }
}

Also make sure it ProjectEntityalso implements INotifyPropertyChanged.

0
source

Source: https://habr.com/ru/post/1765153/


All Articles