0

I am trying to find a best or common practice for creating a table of buttons from a database list.

For my use I am creating more of a Point of Sale type screen. Where I want the categories to load as buttons on the entry screen. The buttons would have a simple task of showing a screen of more dynamically created buttons of the actual items. Those buttons would add the items to, lets call it, a ticket.

The solutions I found were few. I am also trying to code this so others can pick it up fairly quickly. I am extremely rusty and only code once in a while. So I try to follow common ways of doing it. I had some ideas but the code becomes hard to read, mostly because of me.

I saw the below link but was not sure if a ListBox was a good container for this. Dynamic filling WrapPanel buttons from DB, setting the event handlers

I am sure a wrappenel is what I would have to use, but do I put it in a container or use it directly. Do I put it in Xaml or code it all, for issues like spacing between buttons? I think I am overthinking it and need to skip for a little bit.

Thank you,

Community
  • 1
  • 1
  • The answer you linked in question is what you need. You also need to set margin for Button so that you can see some spacing between buttons. It can be done via XAML, then why should you use C#? – Vishal Jul 28 '14 at 21:39
  • Because the rest is in C#. I knew VB in .Net 2.0, VB 6, and VB Applications, but started working with C#. It does work but a Listbox seems unnecessary. Maybe something better/else. – statirasystems Jul 28 '14 at 21:55
  • Rest of the part needs to be in C# or VB. Everything is not possible by just using XAML. The answer here on this question is a good way for you to start. – Vishal Jul 28 '14 at 21:58

1 Answers1

2

It sounds like you want an ItemsControl, bound to your categories, with a WrapPanel as the ItemsPanel. The Button would go in the ItemTemplate.

<ItemsControl ItemsSource="{Binding CategoriesFromDatabase}"> 

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding}" 
                    Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},Path=DataContext.AddToTicketCommand}"
                    CommandParameter="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>

Here I assumed that your view model has properties "CategoriesFromDatabase" (an IEnumerable that you populate from the database), and an ICommand "AddtoTicketCommand" which takes the category as a parameter.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260