当前位置:网站首页>To solve the problem that the data interface is not updated after WPF binding set
To solve the problem that the data interface is not updated after WPF binding set
2020-11-06 21:30:00 【Irving the procedural ape】
solve WPF After binding the set, the data change interface does not update
Independent observers 2020 year 9 month 9 Japan
stay .NET Core 3.1 Of WPF The program is intended to use ListBox Binding displays a collection ( Just meet the demand , It doesn't matter what kind of set ), Here are Xaml Code ( Just a glance , It's not the point of this article ):
<ListBox ItemsSource="{Binding SipRegistrations, Mode=OneWay}" SelectedValue="{Binding SelectedAccountBinding, Mode=OneWayToSource}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding SIPAccount.SIPUsername}"></TextBlock> </DataTemplate> </ListBox.ItemTemplate></ListBox>
ViewModel There's a set of targets in , The present is a List.
There are two ways to implement property change notification , First, use. PropertyChanged.Fody, Second, use custom binding base class BindableBase, Here's the picture .
The following is mainly about data changes ( Collection adds content ) after , There is no problem of updating the front desk interface . To be specific ,List.Add after , The first time it worked , But then it doesn't work , The interface always displays only one piece of data .
original ( No effect ):
SipRegistrations.RemoveAll(x => x.SIPAccount.SIPUsername == sipAccount.SIPUsername); // Remove duplicates ( If any )SipRegistrations.Add(binding); // Add a new item
Guess because List There is no change in the citation of , So it's thought that the attribute hasn't changed , And then there is no change notice .
In fact, this situation requires notice of change , The recommended use is ObservableCollection:
But I used it before ObservableCollection No success , Instead, use List Yes. , So let's take a look at it first List How to solve it .
Variant one ( Debugging has a chance to have an effect ):
// Add contacts to the collection and handle interface binding ;SipRegistrations.RemoveAll(x => x.SIPAccount.SIPUsername == sipAccount.SIPUsername);List<SIPAccountBinding> tempList = SipRegistrations; // Temporary assembly ;SipRegistrations = new List<SIPAccountBinding>(); // The target set is set to empty first ;tempList.Add(binding); // Temporary collection adds new items ;SipRegistrations = tempList; // The temporary set is assigned to the target set ;
Variant one uses temporary variables as a transit , Force the target set ( References to ) Change , But the result is that only in debugging with a very small probability of success .
Because this part of the code is in asynchronous logic , So it could be in a multithreaded environment , and List Not thread safe , So there's variant 2 of the lock version .
Variant two ( No effect , It should be similar to variant 1 ):
#region member /// <summary>/// Lock object /// </summary>private object _lockObj = new object();#endregion// Lock ;lock (_lockObj){ // Add contacts to the collection and handle interface binding ; SipRegistrations.RemoveAll(x => x.SIPAccount.SIPUsername == sipAccount.SIPUsername); List<SIPAccountBinding> tempList = SipRegistrations; SipRegistrations = new List<SIPAccountBinding>(); tempList.Add(binding); SipRegistrations = tempList;}
It doesn't work with a lock ( But locks are needed ), I think of , Since there is a chance of success when debugging , So is it related to the speed of code running ? So a thread sleep is added between the target set's null and its reassignment , You can really , This is the following Variant 3 .
Variant three ( Effective ):
lock (_lockOb.........
版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
边栏推荐
- Open source a set of minimalist front and rear end separation project scaffold
- With this artifact, quickly say goodbye to spam messages
- Take you to learn the new methods in Es5
- 意外的元素..所需元素..
- C and C / C + + mixed programming series 5 - GC collaboration of memory management
- What kind of music do you need to make for a complete game?
- Introduction to the development of small game cloud
- Swagger 3.0 brushes the screen every day. Does it really smell good?
- Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
- An article taught you to download cool dog music using Python web crawler
猜你喜欢
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)
检测证书过期脚本
What are the highlights of Huawei mate 40 series with HMS?
Some operations kept in mind by the front end foundation GitHub warehouse management
迅为-iMX6ULL开发板上配置AP热点
EOS founder BM: what's the difference between UE, UBI and URI?
2020-08-29:进程线程的区别,除了包含关系之外的一些区别,底层详细信息?
C and C / C + + mixed programming series 5 - GC collaboration of memory management
行为型模式之备忘录模式
随机推荐
Pn8162 20W PD fast charging chip, PD fast charging charger scheme
检测证书过期脚本
Code generator plug-in and creator preform file analysis
The isolation level of transaction and its problems
Unity performance optimization
Visual rolling [contrast beauty]
window系统 本机查找端口号占用方法
How much disk space does a new empty file take?
[self taught unity2d legendary game development] map editor
非易失性MRAM存储器应用于各级高速缓存
Open source a set of minimalist front and rear end separation project scaffold
ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录
es创建新的索引库并拷贝旧的索引库 实践亲测有效!
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
2020 database technology conference helps technology upgrade
Take you to learn the new methods in Es5
Swagger 3.0 brushes the screen every day. Does it really smell good?
In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
(2) ASP.NET Core3.1 Ocelot routing
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办