1
votes

I'm toying with this for 1 day already and I'm about the give it up. Basically the point is I got a ListBox containing custom objects (Products) wich are coming from a database. Filling and sorting works but getting the values out of the selected item not.

Product class:

public class Product {   
//properties
public int ID { get; set; }
public String Name { get; set; }
public String Info { get; set; }
public int Stock { get; set; }
public float Price { get; set; }
}

XAML from ListBox in frmUser:

<Window.Resources>
    <DataTemplate x:Key="listBoxTemplate">
        <StackPanel Margin="4">
            <DockPanel >
                <TextBlock FontWeight="Bold" Text="Product ID: " DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="White" FontSize="13" />
                <TextBlock Text="{Binding productId}" Foreground="White" />
            </DockPanel>
            <DockPanel >
                <TextBlock FontWeight="Bold" Text="Naam: " DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="White" FontSize="13"/>
                <TextBlock Text="{Binding naam}" />
            </DockPanel>
            <DockPanel >
                <TextBlock FontWeight="Bold" Text="Omschrijving: " DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="White" FontSize="13"/>
                <TextBlock Text="{Binding omschrijving}" />
            </DockPanel>
            <DockPanel >
                <TextBlock FontWeight="Bold" Text="Voorraad: " DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="White" FontSize="13"/>
                <TextBlock Text="{Binding voorraad}" />
            </DockPanel>
            <DockPanel >
                <TextBlock FontWeight="Bold" Text="Prijs: " DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="White" FontSize="13"/>
                <TextBlock Text="{Binding prijs}" />
            </DockPanel>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

And the binding is done once a user clicks to fill the ListBox:

private void fillDataGrid(object sender, RoutedEventArgs e)
    {
        try
        {
            BindingOperations.ClearAllBindings(lboxProducts);
            dataset = dbWebservice.getDataFromTable(selectedTable);
            lboxProducts.ItemsSource = dataset.Tables[selectedTable].DefaultView;
            countProducts = 0;
            setLabels("0", "0.0");
            btnSort.IsEnabled = true;
            btnBuy.IsEnabled = true;
        }
        catch (Exception ex)
        {
            logger.WriteLine(applicationName, ex.Message);
        }
    }

None...

Now, once the user clicks on 'Buy' at the button I want the selected item (if any is selected) to be placed in a list or whatever, and read out the PRICE. So I can use that value to assign to the total price label. If a user clicks once more, read again the price from the selecteditem and sum it with the previous item. Simply as that. But I can't get in any surcamstance get the values 'stored' in the listboxitems wich are basically 'Product' objects.

At the very end I want to use the 'bought & selected" items their ID's to store back to a tblOrders in the database. That's why I need the ID and PRICE values.

My solution was something like this:

ClassObjects.Product product = (ClassObjects.Product)lboxProducts.SelectedItem;
            float price = product.Price;
            //go on and sum or store the price blabla

But it gives an error it cannot convert from DataRowView to ClassObjects.Product...

Thanks for any help!

2

2 Answers

1
votes

If you use DataTables you get these wrappers, cast to DataRowView first and get the product from there (using for example Item[String]).

1
votes

Rather than setting the itemSource to a dataview, you could always try going through the rows retrieved from your database and creating a Product object for each row returned and putting them into a list. Then set the itemsource to this list. Then when you retrieve your selectedItem, it will be of type ClassObjects.Product rather than a datarowview.