Home > database >  Set background color of DataList item when opening app
Set background color of DataList item when opening app

Time:11-15

When I start an app there's no Item selected on the menu. Tho I'm starting on the main page. How do I make it so its background changed to select like? For now only when I reselect this menu item its background color gets as I want it to be. This code isn't mine but I cant contact the original developer. If anyone needs any additional info please comment. Thank you in advance.

public List<IPageViewModel> PageViewModels
    {
        get
        {
            if (_pageViewModels == null)
                _pageViewModels = new List<IPageViewModel>();

            return _pageViewModels;
        }
    }

    public IPageViewModel CurrentPageViewModel
    {
        get => _currentPageViewModel;
        set
        {
            _currentPageViewModel = value;
            OnPropertyChanged("CurrentPageViewModel");

        }
    }

    private List<MenuItem> menu;

    public List<MenuItem> Menu
    {
        get => menu;
        set
        {
            menu = value;
            OnPropertyChanged("Menu");
        }
    }

    private MenuItem selectedMenuItem;

    public MenuItem SelectedMenuItem
    {
        get => selectedMenuItem;
        set
        {
            selectedMenuItem = value;
            OnPropertyChanged("SelectedMenuItem");
        }
    }

    private RelayCommand goToItemCommand;

    public RelayCommand GoToItemCommand
    {
        get
        {
            return goToItemCommand ??= new RelayCommand(obj =>
            {
                switch (SelectedMenuItem?.Item)
                {
                    case "НОВОСТИ":
                        CurrentPageViewModel = PageViewModels[0];
                        break;
                    case "СЕРВЕРА":
                        CurrentPageViewModel = PageViewModels[1];
                        break;
                    case "ПАРАМЕТРЫ":
                        CurrentPageViewModel = PageViewModels[2];
                        break;
                }
            });
        }
    }

    public GeneralViewModel()
    {
        Menu = new List<MenuItem>()
        {
            new MenuItem(){Item = "НОВОСТИ", IsEnabled = true},
            new MenuItem(){Item = "СЕРВЕРА", IsEnabled = true},
            new MenuItem(){Item = "ПАРАМЕТРЫ", IsEnabled = true}
        };
        PageViewModels.Add(new NewsPageViewModel());
        PageViewModels.Add(new ServersPageViewModel());
        PageViewModels.Add(new ParametersPageViewModel());


        CurrentPageViewModel = PageViewModels[1];
        StartLauncher();

    }

CodePudding user response:

You must initialize the SelectedMenuItem property from the constructor. For example:

public GeneralViewModel()
{
    Menu = new List<MenuItem>()
    {
        new MenuItem(){Item = "НОВОСТИ", IsEnabled = true},
        new MenuItem(){Item = "СЕРВЕРА", IsEnabled = true},
        new MenuItem(){Item = "ПАРАМЕТРЫ", IsEnabled = true}
    };

    // Pre-select a default menu item
    SelectedMenuItem = Menu[0];

    ...
}
  • Related