我有一个发票表,我像这样加载数据(并且运行良好):
private ObservableCollection<InvoiceData> _listAchats;
public ObservableCollection<InvoiceData> ListAchats
{
get { return _listAchats; }
set
{
_listAchats = value;
NotifyPropertyChanged("ListAchats");
}
}
视图模型:
_listAchats = new ObservableCollection<InvoiceData>();
ListAchats = GetAchatList();
public ObservableCollection<InvoiceData> GetAchatList()
{
//Count = 0;
ObservableCollection<InvoiceData> ClientsList = new ObservableCollection<InvoiceData>();
using (SQLiteConnection conn =
new SQLiteConnection(ConfigurationManager.ConnectionStrings["StringConnexion"].ConnectionString))
{
conn.Open();
SQLiteCommand cmdNotes =
new SQLiteCommand("SELECT * FROM Invoice", conn);
// cmdNotes.Parameters.AddWithValue("@MAT_EMP", Matricule);
using (SQLiteDataReader reader = cmdNotes.ExecuteReader())
{
var ordinals = new
{
InvoiceId = reader.GetOrdinal("invoice_id"),
SellerName = reader.GetOrdinal("seller_name"),
PurchaseDate = reader.GetOrdinal("purchase_date")
};
while (reader.Read())
{
var temp = new TableInvoice();
temp.IdInvoice = reader.GetInt32(ordinals.InvoiceId);
temp.SellerName = reader.GetString(ordinals.SellerName);
temp.PurchaseDate = reader.GetDateTime(ordinals.PurchaseDate).ToShortDateString();
// temp.JoinDateString = RelativeTime.ConvertDate(reader.GetDateTime(ordinals.JoinDate));
ClientsList.Add(temp.ClientsList());
}
}
}
return ClientsList;
}
现在,我想做的是,每当我填写某个表单(将其插入到数据库表中)时,都要更新和刷新这个ObservableCollection。我有一个按钮来执行此操作:
private void Refresh()
{
Console.WriteLine("This is triggered");
ListAchats.Clear();
ListAchats = GetAchatList();
Console.WriteLine(ListAchats.Count);
}
DataGrid变空...但由于某种原因无法再次重新加载。您对可能的原因有什么想法吗?这是XAML:
<DataGrid Margin="0 16 0 0"
Name="Agrid"
Background="Transparent"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
AutoGenerateColumns="False"
Focusable="False"
MaxHeight="400"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
CanUserSortColumns="True"
SelectedItem="{Binding SelectedAchat}"
ItemsSource="{Binding ListAchats,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CanUserAddRows="False"
IsReadOnly="True" SelectionMode="Single" SelectionUnit="FullRow">
<DataGrid.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DeleteButtonCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTemplateColumn SortMemberPath="IdInvoice" Header="N° Achat">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Text="{Binding Path=IdInvoice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn SortMemberPath="SellerName" Header="Nom de vendeur" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Text="{Binding Path=SellerName, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn SortMemberPath="PurchaseDate" Header="Date d'achat" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Text="{Binding Path=PurchaseDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
正如我之前所说的,ObservableCollection在我第一次运行应用程序时工作,只是在我刷新它时不工作。
2条答案
按热度按时间fafcakar1#
从你的解释,我敢假设你有问题的变化通知。
请尝试以下实现:
第一个
siotufzp2#
很显然,我忘了在ViewModel中添加
INotifyPropertyChanged
接口,这就是问题的原因。