views:

134

answers:

1

I have a table:

alt text

I'm using NHibernate. The class of entity:

public class PurchasedItem
{
    public virtual int Id { get; set; }
    public virtual Product Product { get; set; }
    public virtual int SortSale { get; set; }
}

I want to get all the records table PurchasedItems (my method returns an IList ). Records are sorted in descending order (column SortSale). How to fill WrapPanel buttons from the list IList ? For each button assign the event handler. By pressing the button display a message with the name of the product.

+2  A: 

You'll need to create a listbox using a WrapPanel as the ItemsPanel. In XAML you can do the following:

<ListBox Name="MyList" ItemsSource={StaticResource YourList}>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Height="40" MinWidth="40" Content="{Binding Id}" Click="Button_Click"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

In this example, I assume YourList is a list of PurchasedItem's. You can also set the ItemsSource from code (ie: MyList.ItemsSource = YourList;). When you click the Button, it'll call the following which can display a messagebox containing whatever you need:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(((sender as Button).DataContext as PurchasedItem).Product.Name);
    }

Note that I set the Content of the Button to the Id of the PurchasedItem so you'll probably want to change that.

Mark Synowiec
Thank you! Good answer!
Anry