Это мой класс:
[Table]
public class ListData : INotifyPropertyChanged, INotifyPropertyChanging
{
private int _id;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int Id
{
get
{
return _id;
}
set
{
if (_id != value)
{
OnPropertyChanging("Id");
_id = value;
OnPropertyChanged("Id");
}
}
}
private string _name;
[Column()]
public string Name
{
get
{ return _name; }
set
{
if (_name != value)
{
OnPropertyChanging("Name");
_name = value;
OnPropertyChanged("Name");
}
}
}
private string _url;
[Column()]
public string Url
{
get
{
return _url;
}
set
{
if (_url != value)
{
OnPropertyChanging("Url");
_url = value;
OnPropertyChanged("Url");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
private void OnPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
}
public class ListDataContext : DataContext
{
public static string DBConnectionString = "Data Source=isostore:/ListDataDB.sdf";
public ListDataContext(string sConnectionString)
: base(sConnectionString)
{ }
public Table<ListData> Datas;
}
это часть моего xaml (списка):
<ListBox x:Name="listData" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="{Binding Path=Name}"
Style="{StaticResource PhoneTextTitle2Style}"
x:Name="txbName"
Tap="txbName_Tap">
</TextBlock>
<Button Tag="{Binding BindsDirectlyToSource=True}"
Click="Button_Click"
Content="X"
BorderBrush="Red"
Background="Red"
Foreground="{StaticResource PhoneBackgroundBrush}"
VerticalAlignment="Top" BorderThickness="0" Width="70" Height="70">
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
вот мои методы загрузки данных:
public void LoadData()
{
this.listData.ItemsSource = GetData();
}
public ObservableCollection<ListData> GetData()
{
List<ListData> listDatas = new List<ListData>();
using (var db = new ListDataContext(ConnectionString))
{
var query = from e in db.Datas
select e;
listDatas = query.ToList();
}
return new ObservableCollection<ListData>(listDatas);
}
и вот как я их удаляю:
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
var data = (ListData)((Button)sender).Tag;
using (var db = new ListDataContext(ConnectionString))
{
var entities = from i in db.Datas
where i.Name == data.Name
select i;
var entity = entities.FirstOrDefault();
db.Datas.DeleteOnSubmit(entity);
db.SubmitChanges();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
В чем проблема? Почему мой ListBox не обновляется после удаления? Есть ли какая-то проблема с ObservableCollection?