Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Delay property WPF - I didn't notice any difference

Tags:

delay

binding

wpf

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?

like image 688
Leonardo Alves Machado Avatar asked Oct 01 '15 12:10

Leonardo Alves Machado


1 Answers

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.

like image 74
Hopeless Avatar answered Nov 03 '22 08:11

Hopeless