c# - Why is selected value null in wpf listbox? -


i have connected database using entity framework , put make , model of cars listbox in format: make - model.

when select item listbox id of object use in query display full details of specific car(row in database) in textblock.

when select item listbox throws null value at:

int id = (int)ltbxavailablecars.selectedvalue;

in xaml, have specified:

listbox selectedvaluepath="id"

are there obvious mistakes here i'm not seeing?

thank in advance.

public partial class mainwindow : window {     carrentaldbentities db = new carrentaldbentities();      public enum cartype { all, small, medium, large }      public mainwindow()     {         initializecomponent();          cbxcartype.itemssource = enum.getvalues(typeof(cartype));     }      private void btnsearch_click(object sender, routedeventargs e)     {         cartype type;         enum.tryparse<cartype>(cbxcartype.selectedvalue.tostring(), out type);          string cartype = type.tostring();          if (cartype == "all")         {             ltbxavailablecars.items.clear();             var cars = c in db.cars                        select new                        {                            c.id,                            c.make,                            c.model,                            c.size                        };              foreach (var car in cars)             {                 ltbxavailablecars.items.add(car.make + " - " + car.model);             }         }         else         {             ltbxavailablecars.items.clear();             var cars = c in db.cars                        c.size == cartype                        select new                        {                            c.id,                            c.make,                            c.model,                            c.size                        };              foreach (var car in cars)             {                 ltbxavailablecars.items.add(car.make + " - " + car.model);             }         }       }      private void ltbxavailablecars_selectionchanged(object sender, selectionchangedeventargs e)     {         int id = (int)ltbxavailablecars.selectedvalue;          carrentaldbentities db = new carrentaldbentities();          var query = c in db.cars                     c.id == id                     select new                     {                         c.id,                         c.make,                         c.model,                         c.size                     };          tblkselectedcardetails.text = query.tostring();     } } 

here approach case. on opinion mentioned databinding observablecollection seems more elegantly , easily
create class keeping needed values in listbox items

public class car {     public int32 id { get; set; }     public datetime make { get; set; }     public string model { get; set; }      public override string tostring()     {        return string.format("{0} - {1}", this.make, this.model);     } } 

tostring override , return string want shown in listbox.
listbox call .tostring() method of item displayed text

then in linq query create instances of class , add them listbox

ltbxavailablecars.items.clear(); var cars = c in db.cars            c.size == cartype            select new car            {                id = c.id,                make = c.make,                model = c.model            };  foreach (car car in cars) {     ltbxavailablecars.items.add(car); } 

and use in selectionchanged event handler

private void ltbxavailablecars_selectionchanged(object sender, selectionchangedeventargs e) {     car temp = (car)ltbxavailablecars.selecteditem;     int32 id = temp.id     carrentaldbentities db = new carrentaldbentities();      var query = c in db.cars                 c.id == id                 select new                 {                     c.id,                     c.make,                     c.model,                     c.size                 };      tblkselectedcardetails.text = query.tostring(); } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -