[英]ListView not updating when the bound ObservableCollection changes
[英]Listview item is not updating when there is changes in Sqlite Db
如果数据库中有一些更改,我正在尝试刷新 Listview 项目。 但我发现的唯一工作方法是将项目添加到我的列表中,然后删除它们并使用计时器再次添加。
Contacts.Clear();
DbModule.LoadLine().ForEach(es => Contacts.Add(es));//Add new items to list from DB.
或使用此代码。
CollectionViewSource.GetDefaultView(Contacts).Refresh();
在我正在使用的列表视图中 . 所以对于这种情况,当我使用计时器时,它会刷新项目并破坏我的文本 animation。 我认为我使用 INotifyPropertyChanged 错误,这应该是原因。 有人可以帮我弄清楚这个吗:D
.xaml 代码
<ListView ItemsSource="{Binding Contacts}"
SelectedItem="{Binding SelectedContact}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Background="#FF2F3136"
BorderThickness="0"
Grid.Row="0"
ItemContainerStyle="{StaticResource ContactCard}">
<ListView.Resources>
<Style TargetType="Label">
<EventSetter Event="MouseRightButtonUp" Handler="OnMouseRightButtonUp"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<!--<StackPanel Orientation="Horizontal" />-->
<WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
model主视图:
class MainViewModel : ObservableObject
{
public ObservableCollection<ContactModel> Contacts { get; set; }
public MainViewModel()
{
Contacts = new ObservableCollection<ContactModel>();
DbModule.LoadLine().ForEach(es => Contacts.Add(es));//Add new items to list from DB.
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(60);
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
foreach (var x in DbModule.LoadLine())
{
Contacts.First(d => d.Line == x.Line).Code = x.Code;
Contacts.First(d => d.Line == x.Line).Batch = x.Batch;
}
OnPropertyChanged();
CollectionViewSource.GetDefaultView(Contacts).Refresh();
}
}
class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
联系 Model
public class ContactModel
{
public int Line { get; set; }
public string Code { get; set; }
public string Batch { get; set; }
public string Message1 { get; set; }
public string Message2 { get; set; }
public int Stacks { get; set; }
public int StacksActual { get; set; }
public int Units { get; set; }
public int UnitsActual { get; set; }
}
您需要提供已更改的属性名称的值
OnPropertyChanged(nameof(Contacts));
完整代码:
class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<ContactModel> Contacts { get; set; }
public MainViewModel()
{
Contacts = new ObservableCollection<ContactModel>();
DbModule.LoadLine().ForEach(es => Contacts.Add(es));//Add new items to list from DB.
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(60);
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
foreach (var x in DbModule.LoadLine())
{
Contacts.First(d => d.Line == x.Line).Code = x.Code;
Contacts.First(d => d.Line == x.Line).Batch = x.Batch;
}
OnPropertyChanged(nameof(Contacts));
CollectionViewSource.GetDefaultView(Contacts).Refresh();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
将INotifyPropertyChanged 添加到我的 ContactModel修复了我的问题。 非常感谢@emoacht
如果有人需要,来自我的 ContactModel 的示例代码。
class ContactModel : ObservableObject
{
private string _Code;
public string Code
{
get { return _Code; }
set
{
_Code = value;
OnPropertyChanged();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.