打开应用程序时设置DataList项目的背景颜色

Set background color of DataList item when opening app

当我启动应用程序时,菜单上没有 selected 项。寿林开始在主页上。我怎样才能让它的背景变成 select 之类的?现在只有当我重新 select 这个菜单项时,它的背景颜色才会变成我想要的样子。此代码不是我的,但我无法联系原始开发人员。如果有人需要任何其他信息,请发表评论。提前谢谢你。

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();

    }

您必须从构造函数中初始化 SelectedMenuItem 属性。例如:

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];

    ...
}