How to refresh a Binding from another thread ?

Using Microsoft Windows Presentation Foundation and Syncfusion WPF Libraries
Post Reply
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

How to refresh a Binding from another thread ?

Post by PGilbert »

If you fix the following two functions in a )CLEAR WS:

      test;binding;str;types;xaml;xml;⎕USING

⎕USING←'System.IO'
⎕USING,←⊂'System'
⎕USING,←⊂'System.Windows.Markup'
⎕USING,←⊂'System.Xml,system.xml.dll'
⎕USING,←⊂'System.Windows.Controls,WPF/PresentationFramework.dll'

xaml←'<Window '
xaml,←' xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"'
xaml,←' xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" '
xaml,←' xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"'
xaml,←' xmlns:local="clr-namespace:testlibrary" '
xaml,←' Title="Window" Height="300" Width="300"> '
xaml,←' <Grid > '
xaml,←' <StackPanel DataContext="{Binding MyValues}"> '
xaml,←' <TextBox Text="{Binding TextValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox> '
xaml,←' </StackPanel> '
xaml,←' </Grid> '
xaml,←'</Window>'

str←⎕NEW StringReader(⊂xaml)
xml←⎕NEW XmlTextReader str
win←XamlReader.Load xml

props←⎕NS''
props.MyValues←⎕NS''
props.MyValues.TextValue←'this is working'

binding←(2015⌶)'props'

win.DataContext←binding
win.Show


      refreshBind ra
props.MyValues.TextValue←ra


Then if you execute the function 'test' you get a WPF window as expected with the TextBox text as: 'this is working'. After that if you execute the function 'refreshBind' you can change the value of the TextBox as expected. But if you try to refresh the namespace with 'refreshBind&' from another thread it is not working. Is there a way to make this work ?

      test
refreshBind 'this is working'
refreshBind& 'this is NOT working'


Thanks in advance,

Pierre Gilbert
User avatar
JohnD|Dyalog
Posts: 74
Joined: Wed Oct 01, 2008 9:35 am

Re: How to refresh a Binding from another thread ?

Post by JohnD|Dyalog »

Hello Pierre,

This looks like a bug, but there's an easy workaround. Just add another line after the assignment in the function - it doesn't even have to do anything:

Code: Select all

refreshBind ra
props.MyValues.TextValue←ra
⍝hello


The problem is that the binding notifications are kept in a thread-specific queue, and in your code the thread ends before the queue is processed. The extra line gives the interpreter a chance to process the queue before thread termination.

I'll look into a proper fix.

Best Regards,
John Daintree.
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to refresh a Binding from another thread ?

Post by PGilbert »

Thanks John, its working for me too.
Post Reply