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
- <StackPanel>
- <Image>
- <Image.Source>
- <PriorityBinding>
- <Binding Path=»SlowImage» IsAsync=»True» />
- <Binding Path=»DefaultImage» />
- </PriorityBinding>
- </Image.Source>
- </Image>
- </StackPanel>
- public Window1()
- {
- InitializeComponent();
- DefaultImage = new BitmapImage(new Uri(«http://slowimage/logo.png»));
- SlowImage = new BitmapImage(new Uri(«http:/defaultimage/logo.png»));
- this.DataContext = this;
- }
- private BitmapImage myDefaultImage;
- public BitmapImage DefaultImage
- {
- get { return this.myDefaultImage; }
- set
- {
- this.myDefaultImage = value;
- this.NotifyPropertyChanged(«Image»);
- }
- }
- private BitmapImage mySlowImage;
- public BitmapImage SlowImage
- {
- get
- {
- Thread.Sleep(5000);
- return this.mySlowImage;
- }
- set
- {
- this.mySlowImage = value;
- this.NotifyPropertyChanged(«SlowImage»);
- }
- }
- #region INotifyPropertyChanged Members
- private void NotifyPropertyChanged(String info)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(info));
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- #endregion
- }
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
- <Window x:Class=»PriorityBindingTest.Window1″
- xmlns=»http://schemas.microsoft.com/winfx/2006/xaml/presentation»
- xmlns:x=»http://schemas.microsoft.com/winfx/2006/xaml»
- Title=»Priority Binding Test» Height=»100″ Width=»200″
- Name=»MainWindow»>
- <Grid>
- <TextBlock HorizontalAlignment=»Center» VerticalAlignment=»Center»>
- <TextBlock.Text>
- <PriorityBinding>
- <Binding ElementName=»MainWindow» Path=»Slow» IsAsync=»True» />
- <Binding ElementName=»MainWindow» Path=»Medium» IsAsync=»True» />
- <Binding ElementName=»MainWindow» Path=»Fast» />
- </PriorityBinding>
- </TextBlock.Text>
- </TextBlock>
- </Grid>
- </Window>
- public partial class Window1 : Window
- {
- public Window1()
- {
- InitializeComponent();
- }
- public string Slow
- {
- get
- {
- Console.WriteLine(«LENTO: « +
- Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(2000);
- return «ESTE ES EL MAS LENTO»;
- }
- }
- public string Medium
- {
- get
- {
- Console.WriteLine(«MEDIO: « +
- Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(1000);
- return «SOY EL DEL MEDIO»;
- }
- }
- public string Fast
- {
- get
- {
- Console.WriteLine(«RAPIDO: « +
- Thread.CurrentThread.ManagedThreadId);
- return «SOY EL MAS RAPIDO»;
- }
- }
- }
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.
En WPF existe el concepto PriorityBinding o se podía decir mejor binding en cascada, es decir podemos
Mmm, un pequeño detalle que no conocía (solía resolverlo con FallbackValue). Me será útil.