Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Straightaway porting is not possible but can we "simulate" this using <code>MultiBinding</code></p> <p>Mind you that this is very tightly coupled solution and may not perform well if many of such bindings are used on a page...</p> <p>Two <strong>must haves</strong> ...</p> <ol> <li>It accepts the delay in milliseconds in a single item <code>ArrayList</code> as a converter parameter. </li> <li>Every such delayed binding must carry its own instance of converter parameter.</li> </ol> <p>The Test XAML is as below...</p> <pre><code> &lt;TextBlock xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib" xmlns:System="clr-namespace:System;assembly=mscorlib" &gt; &lt;TextBlock.Resources&gt; &lt;local:DelayHelper x:Key="DelayHelper"/&gt; &lt;Collections:ArrayList x:Key="MultiConverterParameter"&gt; &lt;System:Int32&gt;2000&lt;/System:Int32&gt; &lt;/Collections:ArrayList&gt; &lt;/TextBlock.Resources&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding UpdateSourceTrigger="LostFocus" Converter="{StaticResource DelayHelper}" ConverterParameter="{StaticResource MultiConverterParameter}"&gt; &lt;Binding Path="Text" ElementName="MyTextBox" Mode="OneWay" /&gt; &lt;Binding RelativeSource="{RelativeSource Self}"/&gt; &lt;Binding BindsDirectlyToSource="True" Source="{x:Static TextBlock.TextProperty}"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;TextBox x:Name="MyTextBox" Text="Test..."/&gt; </code></pre> <p>In this example a <code>TextBlock</code> renders of what is typed in <code>TextBox</code> below after a 2 seconds delay. The <code>TextBox.Text</code> is primary source of data.</p> <p><code>DelayHelper</code> is multi converter that works as shown below...</p> <pre><code>public class DelayHelper : IMultiValueConverter { #region IMultiValueConverter Members public object Convert( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var sourceElement = values[1] as FrameworkElement; var dp = values[2] as DependencyProperty; var paramArray = parameter as ArrayList; var existingValue = paramArray != null &amp;&amp; paramArray.Count == 2 ? paramArray[1] : sourceElement.GetValue(dp); var newValue = values[0]; var bndExp = BindingOperations.GetMultiBindingExpression(sourceElement, dp); var temp = new DispatcherTimer() { IsEnabled = false }; var dspTimer = new DispatcherTimer( new TimeSpan(0,0,0,0, int.Parse(paramArray[0].ToString())), DispatcherPriority.Background, new EventHandler( delegate { if (bndExp != null &amp;&amp; existingValue != newValue) { var array = bndExp.ParentMultiBinding.ConverterParameter as ArrayList; var existingInterval = array[0]; array.Clear(); array.Add(existingInterval); array.Add(newValue); bndExp.UpdateTarget(); } temp.Stop(); }), sourceElement.Dispatcher); temp = dspTimer; dspTimer.Start(); return existingValue; } public object[] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion } </code></pre> <p>So this code makes use of the facts that </p> <ol> <li>MultiBinding can accept the target UI element (<code>TextBlock</code>) and its dependency property (<code>TextBlock.TextProperty</code>) that itself is multi-bound.</li> <li>Once bound the multi binding cannot alter its properties including the <code>ConveterParameter</code>. But the converter parameter itself can be a reference object that maintains its reference throughout the binding is active e.g. <code>ArrayList</code>.</li> <li>The <code>DispatcherTimer</code> has to stop after its first <code>Tick</code>. Hence we use of the <code>temp</code> variable is very essential.</li> <li>The updates make 2 converter passes for each source text update. There is no escpae from this behavior. This may cause slowness is many delayed bindings are used.</li> <li><strong>Make sure you do not share the same converter parameter among multiple delayed bindings</strong> </li> </ol> <p>Let me know if this helps...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload