如果資料庫中有一些更改,我正在嘗試重繪 Listview 專案。但我發現的唯一作業方法是將專案添加到我的串列中,然后洗掉它們并使用計時器再次添加。
Contacts.Clear();
DbModule.LoadLine().ForEach(es => Contacts.Add(es));//Add new items to list from DB.
或使用此代碼。
CollectionViewSource.GetDefaultView(Contacts).Refresh();
在串列視圖中,我正在使用
. 所以對于這種情況,當我使用計時器時,它會重繪 專案并破壞我的文本影片。我認為我使用 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>
主視圖模型:
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));
}
}
聯系方式
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; }
}
uj5u.com熱心網友回復:
將INotifyPropertyChanged添加到我的 ContactModel修復了我的問題。非常感謝@emoacht
如果有人需要,來自我的 ContactModel 的示例代碼。
class ContactModel : ObservableObject
{
private string _Code;
public string Code
{
get { return _Code; }
set
{
_Code = value;
OnPropertyChanged();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406828.html
標籤:
