0

I'm trying to create a data template for a dashboard menu. It has 4 four options for different menus. So I decided to go with data templates. Here so the gist of my code.

xaml

<StackPanel Grid.Row="1" x:Name="ControlPanelMenuContainer">
     <ItemsControl ItemsSource="{Binding MenuItemsNeeded}" Name="DashboardButtonList">
         <ItemsControl.ItemTemplate>
             <DataTemplate>
                 <Button Command="{Binding Command}">
                     <Button.Content>
                        <Grid>
                           <TextBlock Text="{Binding Text}"></TextBlock>
                           <UserControl:Icomoon IcoIcon="{Binding Icon}" />
                        </Grid>
                     </Button.Content>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

code behind Menu Items Class

  class MenuItems
        {
            private ICommand _PrivateCommandName;
            private string _PrivateIcon;
            private string _PrivateTitle;

            public ICommand CommandName
            {
                get { return _PrivateCommandName; }
                private set { _PrivateCommandName = value; }
            }

            public string Icon
            {
                get { return _PrivateIcon; }
                private set { _PrivateIcon = value; }
            }

            public string Title 
            {
                get { return _PrivateTitle; }
                private set { _PrivateTitle = value; }
            }
            public MenuItems(string title, string icon, ICommand command)
            {
                Icon = icon;
                Title = title;
                CommandName = command;
            }
        }

Code Behind the Condensed Main object

MVVM.IControllerMVVM MVVM = new MVVM.IControllerMVVM();
List<MenuItems> MenuItemsNeeded = new List<MenuItems>();


if(User_Who_IsLoggedOn == 0)
{
MenuItemsNeeded.Add(new MenuItems("Data Matrix", "Document", MVVM.GotoDataMatrixDowntimeCommand));
MenuItemsNeeded.Add(new MenuItems("Documents", "Document", MVVM.GotoDocumentsCommand));
MenuItemsNeeded.Add(new MenuItems("Inventory", "FilledBox", MVVM.GotoInventoryCommand));
MenuItemsNeeded.Add(new MenuItems("CorkBoard", "Comment", MVVM.GotoCorkboardCommand));
MenuItemsNeeded.Add(new MenuItems("Quality Assurance", "Measure", MVVM.GotoQCQACommand));
MenuItemsNeeded.Add(new MenuItems("Statistics", "LineGraph", MVVM.GotoStatisticsCommand));
MenuItemsNeeded.Add(new MenuItems("Safety", "Warning", MVVM.GotoSafetySheetCommand));
MenuItemsNeeded.Add(new MenuItems("Calendar", "Warning", MVVM.GotoEditorCommand));
}

So as you can see in the XAML part of the code I have bound all my properties to the appropriate objects. yet nothing shows up properly. This is my first time using the items control object, and yes I am still learning Bindings. I have been watching videos and reading up on articles online and on stackoverflow. But none of which I believe fit my needs. Which is a binding with a binding and more. Right now I'm not quite sure why nothing is even populating the stackpanel at all.

EDIT as to PHOENIX Answer

I've added a public property for MenuItemsNeeded

private List<MenuItems> _MenuItemsNeeded = new List<MenuItems>();

public  List<MenuItems> MenuItemsNeeded
{
    get { return _MenuItemsNeeded; }
}

Everything works except the binding for the Command name section now.

<Button Height="40" Grid.Row="1" Style="{StaticResource NavButton}" Command="{Binding CommandName}">

EasyBB
  • 6,176
  • 9
  • 47
  • 77

1 Answers1

0

Ensure that MenuItemsNeeded is a public Property not an instance or local variable.
Ensure that you have a datacontext that includes MenuItemsNeeded on your window. If MenuItemsNeeded is populated after the screen has been rendered then change MenuItemsNeeded from a List to an ObservableCollection.

ETA:have you checked that the commands have actually been set in your mvvm instance before you create the menu items. As if they've been set afterwards then your menu items will have a command of null.

  • I'm getting do not declare visible instance fields – EasyBB Apr 01 '20 at 16:25
  • I've put examples for how to declare properties in this answer https://stackoverflow.com/questions/60972903/is-a-dispatcher-needed-to-change-data-bound-properties-in-wpf-net-core-3-or-new/60973309#60973309 – Phoenix Stoneham Apr 01 '20 at 16:33