当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- A concise tutorial for Nacos, ribbon and feign
- 嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
- Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
- Visual rolling [contrast beauty]
- Description of phpshe SMS plug-in
- git远程库回退指定版本
- DC-1 target
- Take you to learn the new methods in Es5
- Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
- Python basic data type -- tuple analysis
猜你喜欢
面试官: ShardingSphere 学一下吧
Flink's datasource Trilogy: direct API
JVM memory allocation - xms128m - xmx512m - XX: permsize = 128M - XX: maxpermsize = 512M
Zero basis to build a web search engine of its own
Take you to learn the new methods in Es5
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
C calls SendMessage to refresh the taskbar icon (the icon does not disappear at the end of forcing)
2020-08-14:数据任务的执行引擎用的哪些?
[forward] how to view UserData in Lua
Understanding formatting principles
随机推荐
An article will introduce you to HTML tables and their main attributes
How does cglib implement multiple agents?
Contract trading system development | construction of smart contract trading platform
File download manager realized by electron
Junit测试出现 empty test suite
Metersphere developer's Manual
list转换map(根据key来拆分list,相同key的value为一个list)
From overseas to China, rancher wants to do research on container cloud market
Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?
Multi robot market share solution
An article takes you to understand CSS3 picture border
Zero basis to build a web search engine of its own
2020 database technology conference helps technology upgrade
How to play sortable JS vuedraggable to realize nested drag function of forms
[elastic search engine]
事务的本质和死锁的原理
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
C language I blog assignment 03
Python 100 cases
Novice guidance and event management system in game development