PriorityBinding o Binding en Cascada

En WPF existe el concepto PriorityBinding o se podía decir mejor binding en cascada, es decir podemos hacer un binding a diferentes propiedades indicándolas de mas deseable a menos deseable. Si la primera tiene el valor nulo o falla, coge el segundo binding, con la explicación que me cascado complicado de ver, lo mejor un ejemplo

  1.     <StackPanel>
  2.         <Image>
  3.             <Image.Source>
  4.                 <PriorityBinding>
  5.                     <Binding Path=»SlowImage» IsAsync=»True» />
  6.                     <Binding Path=»DefaultImage» />
  7.                 </PriorityBinding>
  8.             </Image.Source>
  9.         </Image>
  10.     </StackPanel>
  1.   public Window1()
  2.             {
  3.                 InitializeComponent();
  4.                 DefaultImage = new BitmapImage(new Uri(«http://slowimage/logo.png»));
  5.                 SlowImage = new BitmapImage(new Uri(«http:/defaultimage/logo.png»));
  6.                 this.DataContext = this;
  7.             }
  8.             private BitmapImage myDefaultImage;
  9.             public BitmapImage DefaultImage
  10.             {
  11.                 get { return this.myDefaultImage; }
  12.                 set
  13.                 {
  14.                     this.myDefaultImage = value;
  15.                     this.NotifyPropertyChanged(«Image»);
  16.                 }
  17.             }
  18.             private BitmapImage mySlowImage;
  19.             public BitmapImage SlowImage
  20.             {
  21.                 get
  22.                 {
  23.                     Thread.Sleep(5000);
  24.                     return this.mySlowImage;
  25.                 }
  26.                 set
  27.                 {
  28.                     this.mySlowImage = value;
  29.                     this.NotifyPropertyChanged(«SlowImage»);
  30.                 }
  31.             }
  32.             #region INotifyPropertyChanged Members
  33.             private void NotifyPropertyChanged(String info)
  34.             {
  35.                 if (PropertyChanged != null)
  36.                 {
  37.                     PropertyChanged(this, new PropertyChangedEventArgs(info));
  38.                 }
  39.             }
  40.             public event PropertyChangedEventHandler PropertyChanged;
  41.             #endregion
  42.         }

 

En este caso tenemos una imagen en la que indicamos que nuestro binding mas deseable es con la propiedad SlowImage  pero que si se produce un error o esta vacia le asigne el binding DefaultImage. Es importante que os fijéis en la propiedad IsAsync, es necesario que este a true, esto le dice a WPF que utilice otro thread para devolver esta propiedad. es importante para propiedades que tardan bastante en devolver un valor ya que lo importante es que cuando devuelva el valor aunque haya hecho el binding con la siguiente propiedad mostrara el valor de la propiedad que mas deseamos.

Para ver esto ultimo mirar este ejemplo

 

  1. <Window x:Class=»PriorityBindingTest.Window1″
  2.    xmlns=»http://schemas.microsoft.com/winfx/2006/xaml/presentation»
  3.    xmlns:x=»http://schemas.microsoft.com/winfx/2006/xaml»
  4.    Title=»Priority Binding Test» Height=»100″ Width=»200″
  5.    Name=»MainWindow»>
  6.     <Grid>
  7.     <TextBlock HorizontalAlignment=»Center» VerticalAlignment=»Center»>
  8.       <TextBlock.Text>
  9.         <PriorityBinding>
  10.           <Binding ElementName=»MainWindow» Path=»Slow» IsAsync=»True» />
  11.           <Binding ElementName=»MainWindow» Path=»Medium» IsAsync=»True» />
  12.           <Binding ElementName=»MainWindow» Path=»Fast» />
  13.         </PriorityBinding>
  14.       </TextBlock.Text>
  15.     </TextBlock>
  16.     </Grid>
  17. </Window>

 

  1. public partial class Window1 : Window
  2.   {
  3.     public Window1()
  4.     {
  5.       InitializeComponent();
  6.     }
  7.     public string Slow
  8.     {
  9.       get
  10.       {
  11.         Console.WriteLine(«LENTO: « +
  12.           Thread.CurrentThread.ManagedThreadId);
  13.         Thread.Sleep(2000);
  14.         return «ESTE ES EL MAS LENTO»;
  15.       }
  16.     }
  17.     public string Medium
  18.     {
  19.       get
  20.       {
  21.         Console.WriteLine(«MEDIO: « +
  22.           Thread.CurrentThread.ManagedThreadId);
  23.         Thread.Sleep(1000);
  24.         return «SOY EL DEL MEDIO»;
  25.       }
  26.     }
  27.     public string Fast
  28.     {
  29.       get
  30.       {
  31.         Console.WriteLine(«RAPIDO: « +
  32.           Thread.CurrentThread.ManagedThreadId);
  33.         return «SOY EL MAS RAPIDO»;
  34.       }
  35.     }
  36.   }

En el textbox ira mostrando cada uno de los valores primero el Fast, segundo el Medum y por ultimo el que deseamos Slow, si quitásemos IsAsync, no funcionaria y se quedaría con Fast.

2 comentarios sobre “PriorityBinding o Binding en Cascada”

Responder a anonymous Cancelar respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *