I Have one table of data:
tblFeed
Id
Title
Content
And I populated a Listbox in my WPF application with this table.
I have the issue now of using the Id value for an event but the Id keeps returning 0.
Any Suggestions?
WCF
public List<Feed> GetFeed()
{
List<Feed> r = new List<Feed>();
List<Feed> e;
using (TruckDb db = new TruckDb())
e = db.Feed.Where(x => x.Id != null).ToList();
foreach (var a in e)
{
var feed = new Feed()
{
Id = a.Id,
Title = a.Title,
Content = a.Content
};
r.Add(feed);
}
return r;
}
WPF
public async Task LoadFeeds()
{
TruckServiceClient TSC = new TruckServiceClient();
try
{
List<ClientItems> feeditems = new List<ClientItems>();
if (lbFeed.Items.Count <= 0)
foreach (var item in await TSC.GetFeedAsync())
{
feeditems.Add(new ClientItems
{
FId = item.Id,
FTitle = item.Title,
FContent = item.Content
});
}
lbFeed.ItemsSource = (feeditems.ToArray());
lbFeed.DisplayMemberPath = "FTitle";
}
catch (Exception)
{
throw;
}
}
public class ClientItems
{
public int FId { get; set; }
public string FTitle { get; set; }
public string FContent { get; set; }
public override string ToString()
{
return FTitle;
}
}
Delete Event
WCF
private void bnFeedDel_Click(object sender, RoutedEventArgs e)
{
TruckServiceClient service = new TruckServiceClient();
service.DelFeedAsync(new FeedView
{
Id = lbFeed.SelectedIndex
});
}
WPF
public void DelFeed(FeedView feedview)
{
using (var result = new TruckDb())
{
var t = new Feed
{
Id = feedview.Id
};
result.Feed.Remove(t);
result.SaveChanges();
}
}
In your bnFeedDel_Click method you are doing this:
Id = lbFeed.SelectedIndex
I think this is your problem as you don't want to set Id to a SelectedIndex value but rather:
[EDIT after some discussion]
Set SelectedValuePath inside LoadFeeds:
lbFeed.SelectedValuePath = "FId";
And use SelectedValue instead of SelectedIndex:
private void bnFeedDel_Click(object sender, RoutedEventArgs e)
{
TruckServiceClient service = new TruckServiceClient();
service.DelFeedAsync(new FeedView
{
// Of course you may want to check for nulls etc...
Id = (int)lbFeed.SelectedValue;
});
}
Also, you should use DbSet.Attatch() before deleting a record:
public void DelFeed(FeedView feedview)
{
using (var result = new TruckDb())
{
var t = new Feed
{
Id = feedview.Id
};
result.Feed.Attatch(t);
result.Feed.Remove(t);
result.SaveChanges();
}
}
Related
I'm trying to do a related combobox. I already have 2 comboboxes, but now I want to add a third.
I have this code for the 2nd combo box.
I'm using windows forms.
The entire code: https://repl.it/#devilonline/MuddyPartialBytecode#main.cs
private string[] GetCastById(int id)
{
return nomes.Where(line => line.movies_id== id).Select(l => l.nomes).ToArray();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
int id = nomes[comboBox1.SelectedIndex].id;
foreach (string name1 in GetCastById(id))
{
this.comboBox1.Items.Add(name1);
}
}
print
It is obvious that you are clearing the Items of the comboBox1 then try to get the id of the selected item, which should throw an exception because no item will be selected by then:
comboBox1.Items.Clear(); // here the items are cleared
int id = nomes[comboBox1.SelectedIndex].id; // nomes[comboBox1.SelectedIndex] = -1
Based on your database, the cast table is related to the movies so each movie has a corresponding list of cast, you should then get the id of the selected movie rather than the selected nome:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
int id = movies[comboBoxMovie.SelectedIndex].id; // here we used comboBoxMovie
foreach (string name1 in GetCastById(id))
{
this.comboBox1.Items.Add(name1);
}
}
This is a great opportunity to use databinding through a System.ComponentModel.BindingList<>. Below is a working example. Note, I've added get methods inside the classes for simplicity of populating the ComboBoxes for this example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
public class Form1
{
class Category
{
public int Id { get; set; }
public string Name { get; set; }
public Category(int id, string name)
{
this.Id = id;
this.Name = name;
}
public static List<Category> GetCategories()
{
return new List<Category>()
{
new Category(1, "Action"),
new Category(2, "Comedy")
};
}
}
class Movie
{
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public Movie(int id, string name, int catId)
{
this.Id = id;
this.Name = name;
this.CategoryId = catId;
}
public static List<Movie> GetMovies()
{
return new List<Movie>()
{
new Movie(1, "Rambo", 1),
new Movie(2, "Delta Force", 1),
new Movie(3, "Elf", 2),
new Movie(4, "Space Balls", 2)
};
}
}
class Cast
{
public int Id { get; set; }
public string Names { get; set; }
public int MovieId { get; set; }
public Cast(int id, string names, int movieId)
{
this.Id = id;
this.Names = names;
this.MovieId = movieId;
}
public static List<Cast> GetCast()
{
return new List<Cast>()
{
new Cast(1, "Silvester Stalone", 1),
new Cast(2, "Chuck Norris", 2),
new Cast(3, "Will Farrell", 3),
new Cast(4, "John Candy", 4)
};
}
}
private BindingList<Category> _categoryBindingList = new BindingList<Category>();
private BindingList<Movie> _moviesBindingList = new BindingList<Movie>();
private BindingList<Cast> _castBindingList = new BindingList<Cast>();
private void Form1_Load(object sender, EventArgs e)
{
// Your database calls would replace these Get methods.
Category.GetCategories().ForEach(x => _categoryBindingList.Add(x));
Movie.GetMovies().ForEach(x => _moviesBindingList.Add(x));
Cast.GetCast().ForEach(x => _castBindingList.Add(x));
ComboBox1.DataSource = _categoryBindingList;
ComboBox1.DisplayMember = "Name";
ComboBox2.DataSource = _moviesBindingList.Where(x => x.CategoryId == (Category)ComboBox1.SelectedValue.Id).ToList();
ComboBox2.DisplayMember = "Name";
ComboBox3.DataSource = _castBindingList.Where(x => x.MovieId == (Movie)ComboBox2.SelectedValue.Id).ToList();
ComboBox3.DisplayMember = "Names";
}
private void ComboBox1_SelectedValueChanged(object sender, EventArgs e)
{
ComboBox2.DataSource = _moviesBindingList.Where(x => x.CategoryId == (Category)ComboBox1.SelectedValue.Id).ToList();
ComboBox2.DisplayMember = "Name";
ComboBox3.DataSource = _castBindingList.Where(x => x.MovieId == (Movie)ComboBox2.SelectedValue.Id).ToList();
ComboBox3.DisplayMember = "Names";
}
private void ComboBox2_SelectedValueChanged(object sender, EventArgs e)
{
ComboBox3.DataSource = _castBindingList.Where(x => x.MovieId == (Movie)ComboBox2.SelectedValue.Id).ToList();
ComboBox3.DisplayMember = "Names";
}
}
I can't connect my XML file and my C# in microsoft visual studio. How do I solve this?
Here you find what need to be in your DA
public class LeegstaandXML
{
public string Index { get; set; }
public string Aard { get; set; }
public string Adres { get; set; }
public static int intElementsCount;
private List<LeegstaandXML> _leegstaand;
public List<LeegstaandXML> Lleegstaand
{
get
{
XmlDocument doc = new XmlDocument();
doc.Load("Lijst_leegstaande_bedrijfspanden.xml");
XmlNodeList elementlist = doc.GetElementsByTagName("fme:Lijst_leegstaande_bedrijfspanden");
intElementsCount = elementlist.Count;
_leegstaand = new List<LeegstaandXML>();
_leegstaand.Add(new LeegstaandXML()
{
Index = elementlist[Form1.counter]["fme:Dossier_ID"].InnerXml,
Adres = elementlist[Form1.counter]["fme:Adres"].InnerXml,
Aard = elementlist[Form1.counter]["fme:Aard_van_het_gebouw"].InnerXml,
});
return _leegstaand;
}
set
{
_leegstaand = value;
}
}
}
This is your connection in your form
private void btnVullen_Click(object sender, EventArgs e)
{
Leegstaand p = new Leegstaand();
do
{
counter++;
LeegstaandXML leegstaand2 = new LeegstaandXML();
foreach(LeegstaandXML lp in leegstaand2.Lleegstaand)
{
leegstaandDA NewItem = new leegstaandDA(lp.Index, lp.Adres, lp.Aard);
NewItem.AddItem();
//ListViewItem item = new ListViewItem(new string[] { lp.id.ToString(), lp.aard, lp.adres, lp.index.ToString() });
// item.Tag = lp;
//lsvLeegstaand.Items.Add(item);
}
} while (counter < LeegstaandXML.intElementsCount -1);
MessageBox.Show("implementatie is geslaagd");
counter = 0;
leegstaandDA.Getleegstaand();
foreach (Leegstaand l in leegstaandDA.Getleegstaand())
{
ListViewItem item = new ListViewItem(new string[] { l.index.ToString(), l.adres, l.aard});
item.Tag = l;
lsvLeegstaand.Items.Add(item);
}
}
I'm getting this error on DataBind(), and I don't know why since there shouldn't be anything selected.
DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DataTextField = "Title";
DdState.DataValueField = "Title";
DdState.Items.Insert(0, String.Empty);
if (DdState.SelectedItem != null)
{
DdState.SelectedItem.Selected = false;
}
DdState.DataBind();
private IEnumerable<IStateItem> UsStates
{
get
{
var statesFolder = _sitecoreService.GetItem<ISitecoreItem>(ItemReference.BcsUs_ProductData_States.Guid);
if (statesFolder == null)
return new List<IStateItem>();
List<IStateItem> usStates = _sitecoreService.QueryChildren<IStateItem>(statesFolder).OrderBy(s => s.Title).ToList();
return usStates;
}
}
I tried putting in DdState.SelectedIndex = 0 before the DataBind(), but then I got an error that the selected index did not exist. What's going on?
If the DataSource is a list its much easier to implement. So just "convert" the UsStates IEnumerable to a List an then add it to the data source.
DdState.DataSource = UsStates.ToList();
Then choose the property of a list item as binding.
OR
public Form1()
{
InitializeComponent();
DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DisplayMember = "Statename";
DdState.SelectedIndex = 0;
}
private List<IStateItem> UsStates
{
get
{
List<IStateItem> usStates = new List<IStateItem>();
usStates.Add(new IStateItem("California","status1"));
usStates.Add(new IStateItem("Ohio", "status3"));
return usStates;
}
}
private class IStateItem
{
public IStateItem(string statename, string stateStatus)
{
Statename = statename;
StateStatus = stateStatus;
}
public string Statename { get; set; }
public string StateStatus { get; set; }
}
Could there be something wrong with your IStateItem class?
I copy/pasted your code in a new asp.net application, made my own IStateItem class and it works.
using System;
using System.Collections.Generic;
namespace TestIt
{
public partial class Form1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillTheList();
}
private void FillTheList()
{
ddl_TheList.Items.Clear();
ddl_TheList.DataSource = UsStates;
ddl_TheList.DataTextField = "statename";
ddl_TheList.DataValueField = "stateStatus";
//ddl_TheList.Items.Insert(0, String.Empty);
ddl_TheList.DataBind();
ddl_TheList.SelectedIndex = 0;
}
private IEnumerable<IStateItem> UsStates
{
get
{
List<IStateItem> usStates = new List<IStateItem>();
for (int i = 0; i < 10; i++)
{
usStates.Add(new IStateItem { statename = "state #" + i, stateStatus = "cool state bro" });
}
return usStates;
}
}
}
public class IStateItem
{
public string statename { get; set; }
public string stateStatus { get; set; }
}
}
I have a form with 2 combos, among other fields. 1 combo is filled with brand names (cmbMarca). This combo is filled correctly.
The other combo (cmbModelo) should be filled with models of the selected brand.
My problem is that the combo with models (cmbModelo) is not updated when selecting a brand (cmbBrand). When I select a brand, runs all the code but does not display any item in the combo "cmbModelo"
FillingForm.xaml
<Input:SfComboBox x:Name="cmbMarca" x:uid="BrandsCombo"
DisplayMemberPath="marca"
ItemsSource="{Binding MarcasSAT.Marcas}" SelectedValue="{Binding marca, Mode=TwoWay}"
SelectedValuePath="marca"
Tag="{Binding Path=SelectedMarca, Mode=TwoWay}" SelectionChanged="cmbMarca_SelectionChanged"/>
<Input:SfComboBox x:Name="cmbModelo" x:uid="ModelosCombo"
DisplayMemberPath="modelo"
ItemsSource="{Binding ModelosSAT.Modelos}"
SelectedValue="{Binding modelo, Mode=TwoWay}"
SelectedValuePath="modelo" />
FillingForm.CS
private void cmbMarca_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MarcaViewModel curItem = (MarcaViewModel)cmbMarca.SelectedItem;
this.cmbMarca.Tag = curItem.idMarca;
}
FillingViewModel.cs
public class FillingViewModel : ViewModelBase
{
private readonly MarcasViewModel marcasSAT = new MarcasViewModel();
public MarcasViewModel MarcasSAT
{
get { return this.marcasSAT; }
}
private ModelosViewModel modelosSAT = new ModelosViewModel();
public ModelosViewModel ModelosSAT
{
get
{
return this.modelosSAT;
}
set
{
modelosSAT = value;
RaisePropertyChanged("ModelosSAT");
}
}
private int _selectedMarca;
public int SelectedMarca
{
get { return _selectedMarca; }
set
{
_selectedMarca = value;
RaisePropertyChanged("SelectedMarca");
modelosSAT.MarcaID = _selectedMarca;
}
}
}
ModelosViewModel.cs
public class ModelosViewModel : ViewModelBase
{
private ObservableCollection<ModeloViewModel> modelos;
public ObservableCollection<ModeloViewModel> Modelos
{
get
{
return modelos ;
}
set
{
modelos = value;
RaisePropertyChanged("Modelos");
}
}
public ObservableCollection<ModeloViewModel> GetModelos()
{
modelos = new ObservableCollection<ModeloViewModel>();
using (var db = new SQLite.SQLiteConnection(App.DBPath))
{
var query = db.Table<Modelos>().Where(c => c.idMarca == _marcaID);
//var query = db.Table<Modelos>();
foreach (var _mrc in query)
{
var mrc = new ModeloViewModel()
{
idMarca = _mrc.idMarca,
idModelo = _mrc.idModelo,
modelo = _mrc.modelo
};
modelos.Add(mrc);
}
}
return modelos ;
}
private int _marcaID=0;
public int MarcaID
{
get {
return _marcaID;
}
set
{
_marcaID = value;
RaisePropertyChanged("MarcaID");
this.GetModelos();
}
}
//public ModelosViewModel()
//{
// this.modelos = GetModelos();
//}
}
MarcasViewModel.cs
public class MarcasViewModel : ViewModelBase
{
private ObservableCollection<MarcaViewModel> marcas;
public ObservableCollection<MarcaViewModel> Marcas
{
get
{
return marcas ;
}
set
{
marcas = value;
RaisePropertyChanged("Marcas");
}
}
public ObservableCollection<MarcaViewModel> GetMarcas()
{
marcas = new ObservableCollection<MarcaViewModel>();
using (var db = new SQLite.SQLiteConnection(App.DBPath))
{
var query = db.Table<Marcas>();
foreach (var _mrc in query)
{
var mrc = new MarcaViewModel()
{
idMarca = _mrc.idMarca ,
marca = _mrc.marca
};
marcas.Add(mrc);
}
}
return marcas ;
}
public MarcasViewModel()
{
this.marcas = GetMarcas();
}
}
ViewModelBase.cs
public class ViewModelBase
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
UPDATE1:
when I select a brand item, only the next code is called and in this order:
1st (in FillinfgForm.cs)
private void cmbMarca_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MarcaViewModel curItem = (MarcaViewModel)cmbMarca.SelectedItem;
this.cmbMarca.Tag = curItem.idMarca;
}
2nd (in FillingViewModel.cs)
public int SelectedMarca
set
{
_selectedMarca = value;
RaisePropertyChanged("SelectedMarca");
modelosSAT.MarcaID = _selectedMarca;
}
3rd (in ModelosViewModel.cs)
Public int MarcaID
{
set
{
_marcaID = value;
RaisePropertyChanged("MarcaID");
this.GetModelos();
}
}
4th
public ObservableCollection<ModeloViewModel> GetModelos()
{
modelos = new ObservableCollection<ModeloViewModel>();
using (var db = new SQLite.SQLiteConnection(App.DBPath))
{
var query = db.Table<Modelos>().Where(c => c.idMarca == _marcaID);
//var query = db.Table<Modelos>();
foreach (var _mrc in query)
{
var mrc = new ModeloViewModel()
{
idMarca = _mrc.idMarca,
idModelo = _mrc.idModelo,
modelo = _mrc.modelo
};
modelos.Add(mrc);
}
}
return modelos ;
}
The problem is in GetMarcas() methods.
As you can see you work not with the property but with the field - 'marcas'. But when you assign a value to a field (in you particular case marcas = new ObservableCollection();) PropertyChanged method is not invoked. That is why you don't see UI changes.
You should work with the property istead of field or you can clear the existing ObservableCollection and add you values. Both variants should work.
So the following code should work:
public ObservableCollection<MarcaViewModel> GetMarcas()
{
Marcas = new ObservableCollection<MarcaViewModel>();
using (var db = new SQLite.SQLiteConnection(App.DBPath))
{
var query = db.Table<Marcas>();
foreach (var _mrc in query)
{
var mrc = new MarcaViewModel()
{
idMarca = _mrc.idMarca ,
marca = _mrc.marca
};
marcas.Add(mrc);
}
}
return marcas ;
}
or, a better solution:
public ObservableCollection<MarcaViewModel> GetMarcas()
{
Marcas.Clear();
using (var db = new SQLite.SQLiteConnection(App.DBPath))
{
var query = db.Table<Marcas>();
foreach (var _mrc in query)
{
var mrc = new MarcaViewModel()
{
idMarca = _mrc.idMarca ,
marca = _mrc.marca
};
Marcas.Add(mrc);
}
}
return marcas;
}
Also there is a good practice called "do not ignore return values", as you can see you don't need to return ObservableCollection from GetMarcas, so either rewrite it so that it returns nothing or rewrite invoking code (it dependes on your existing code).
I am developing an windows phone app using sqlite database.I am able to show out the database and delete the particular row I want to delete.But the problem is after I select the row and click delete the row does not vanishes at that time.I have to renter that page to see that it is deleted.
Below here is the code of the class where I use the click_delete event
public partial class History : PhoneApplicationPage
{
ObservableCollection<historyTableSQlite> DB_HistoryList = new ObservableCollection<historyTableSQlite>();
DbHelper Db_helper = new DbHelper();
//public static int Selected_HistoryId;
//int Selected_HistoryId;
public static int Selected_HistoryId {get; set;}
// string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
public History()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Db_helper.AddInfo();
ReadHistoryList_Loaded();
// Selected_HistoryId = int.Parse(NavigationContext.QueryString["SelectedHistoryID"]);
}
public void ReadHistoryList_Loaded()
{
ReadAllContactsList dbhistory = new ReadAllContactsList();
DB_HistoryList = dbhistory.GetAllHistory();//Get all DB contacts
ListData.ItemsSource = DB_HistoryList.OrderByDescending(i => i.Id).ToList();
//Latest contact ID can Display first
}
public void ListData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ListData.SelectedIndex != -1)
{
historyTableSQlite listitem = ListData.SelectedItem as historyTableSQlite;
History.Selected_HistoryId = listitem.Id;
}
}
private void Delete_Click(object sender, EventArgs e)
{
Db_helper.DeleteContact(History.Selected_HistoryId);
NavigationService.Navigate(new Uri("/History.xaml", UriKind.Relative));
}
private void DeleteAll_Click(object sender, EventArgs e)
{
DbHelper Db_helper = new DbHelper();
Db_helper.DeleteAllContact();//delete all DB contacts
DB_HistoryList.Clear();//Clear collections
ListData.ItemsSource = DB_HistoryList;
}
}
}
below is the class with all main functions
public class DbHelper
{
SQLiteConnection dbConn;
public async Task<bool> onCreate(string DB_PATH)
{
try
{
if (!CheckFileExists(DB_PATH).Result)
{
using (dbConn = new SQLiteConnection(DB_PATH))
{
dbConn.CreateTable<historyTableSQlite>();
}
}
return true;
}
catch
{
return false;
}
}
private async Task<bool> CheckFileExists(string fileName)
{
try
{
var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
return true;
}
catch
{
return false;
}
}
//retrieve all list from the database
public ObservableCollection<historyTableSQlite> ReadHistory()
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
List<historyTableSQlite> myCollection = dbConn.Table<historyTableSQlite>().ToList<historyTableSQlite>();
ObservableCollection<historyTableSQlite> HistoryList = new ObservableCollection<historyTableSQlite>(myCollection);
return HistoryList;
}
}
// Insert the new info in the histrorytablesqlite table.
public void Insert(historyTableSQlite newcontact)
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
dbConn.RunInTransaction(() =>
{
dbConn.Insert(newcontact);
});
}
}
public void AddInfo()
{
DbHelper Db_helper = new DbHelper();
Db_helper.Insert((new historyTableSQlite
{
Date = DateTime.Now.ToShortDateString(),
Time = DateTime.Now.ToShortTimeString(),
Zone = Checkin.Zone_st,
Floor = Checkin.Floor_st,
latitude = Checkin.Latitud_do,
longtitude = Checkin.Longtitude_do
}));
}
// Delete specific contact
public void DeleteContact(int Id)
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
var existingvalue = dbConn.Query<historyTableSQlite>("select * from historyTableSQlite where Id =" + Id).FirstOrDefault();
if (existingvalue != null)
{
dbConn.RunInTransaction(() =>
{
dbConn.Delete(existingvalue);
});
}
}
}
//Delete all contactlist or delete Contacts table
public void DeleteAllContact()
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
//dbConn.RunInTransaction(() =>
// {
dbConn.DropTable<historyTableSQlite>();
dbConn.CreateTable<historyTableSQlite>();
dbConn.Dispose();
dbConn.Close();
//});
}
}
below is the class with all tables
public class historyTableSQlite : INotifyPropertyChanged
{
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id
{
get;
set;
}
private int idValue;
private string dateValue = string.Empty;
public string Date
{
get { return this.dateValue; }
set
{
if (value != this.dateValue)
{
this.dateValue = value;
NotifyPropertyChanged("Date");
}
}
}
private string timeValue = string.Empty;
public string Time
{
get { return this.timeValue; }
set
{
if (value != this.timeValue)
{
this.timeValue = value;
NotifyPropertyChanged("Time");
}
}
}
private string floorValue = string.Empty;
public string Floor
{
get { return this.floorValue; }
set
{
if (value != this.floorValue)
{
this.floorValue = value;
NotifyPropertyChanged("Floor");
}
}
}
public string zoneValue;
public string Zone
{
get { return this.zoneValue; }
set
{
if (value != this.zoneValue)
{
this.zoneValue = value;
NotifyPropertyChanged("Zone");
}
}
}
private double latValue;
public double latitude
{
get { return latValue; }
set
{
if (value != this.latValue)
{
this.latValue = value;
NotifyPropertyChanged("Latitude");
}
}
}
private double lonValue;
public double longtitude
{
get { return this.lonValue; }
set
{
if (value != this.lonValue)
{
this.lonValue = value;
NotifyPropertyChanged("Longitude");
}
}
}
// public string isMarkPoint { get; set; }
public historyTableSQlite()
{
}
public historyTableSQlite(string date, string time, string floor, string zone, double lat, double lng)
{
Date = date;
Time = time;
Floor = floor;
Zone = zone;
latitude = lat;
longtitude = lng;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
If you delete the item from your ObservableCollection, it will notify the ListBox to update its container.
In your code you have
// this is correct
ObservableCollection<historyTableSQlite> DB_HistoryList = new ObservableCollection<historyTableSQlite>();
But your problem is that you don't actually link your ListBox to it.
In your code you create a copy (and the worst kind of copy given what you're trying to do) and you set the ListBox ItemsSource to it. See below (you have this)
ListData.ItemsSource = DB_HistoryList.OrderByDescending(i => i.Id).ToList();
So basically, your ListBox is not an ObservableCollection but it's a List structure.
Deleting and Inserting into the List will not update the ListBox's UI.
Get rid of this List, find another way to sort your ObservableCollection.
Then you can basically do this
ListData.ItemsSource = DB_HistoryList; // set the listbox to the actual obs collection
DB_HistoryList.RemoveAt(i); // remove the item at index i
DB_HistoryList.RemoveItem(object); // remove the object that matches