10

I was trying to understand more about bindings delay and its effects. I've implemented a simple code, but, honestly, I didn't notice any visual difference in the end, with or without delays. Here is the code:

<Window x:Class="Example00.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<Grid  >   
    <Grid.RowDefinitions >
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBox Name="mySourceElement" Grid.Row="0" >Hello World</TextBox>
    <TextBlock Grid.Row="1">            
        <TextBlock.Text>
            <Binding ElementName="mySourceElement" Path="Text" Mode="TwoWay" Delay="60000" />
        </TextBlock.Text>
    </TextBlock> 

    <TextBlock Text="{Binding ElementName=mySourceElement, Mode=TwoWay, Path=Text, Delay=50000}" Grid.Row="2" />
</Grid>

It is basically a code based on a tutorial from Code Project (http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part - example zero), but using .Net 4.5 and with the delays added. I added a very long delay to visually see the difference, however I didn't notice anything different from not using delays.

I wonder if I misunderstood the property - shouldn't the text on the other textboxes wait the "delay" amount to reflect the change typed by the user on the first textbox?

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53

1 Answers1

13

Yes you misunderstood the Delay a bit. This property is named in a very confusing way. In fact it works only one way, from target to source. Meaning when every change occurred in target, the change updated to source will be delayed. The other way won't work, meaning every change occurs in source won't delay the reflect to the target.

So in this case it should be like this:

<!-- NOTE: we name TextBlock as target but 
     in fact it's the source of the Binding -->
<TextBox Text="{Binding Text, ElementName=target, Mode=TwoWay, 
                UpdateSourceTrigger=PropertyChanged, Delay=1000}"
         ></TextBox>
<TextBlock Grid.Row="1" Name="target">            
</TextBlock> 

In your code, your Binding has source as TextBox, target as TextBlock. So every change in TextBox will reflect immediately to TextBlock without effect of Delay.

Hopeless
  • 4,397
  • 5
  • 37
  • 64
  • I see... So if I would like to have a delay on text updating, what should I do? I tried the piece of code you posted, but It still don`t have the delay effect... – Leonardo Alves Machado Oct 01 '15 at 14:09
  • 1
    @LeonardoAlvesMachado I've tested the code before posting it here, it works on my side, you should double-check it, if you modified the code a little, be sure you understand how it works and if possible update what you tried in your question so that I can check what could be wrong. – Hopeless Oct 01 '15 at 14:11
  • 1
    Yeah, you were right. There was only an error (compilation time) because the field Text was defined more than once. but removing the Hello World string made it work. Thanks a lot – Leonardo Alves Machado Oct 01 '15 at 14:15