Quitar Converter de WPF con BindingBase.StringFormat

La propiedad BindingBase.StringFormat es nueva en WPF 4.0 y nos va ayudar a reducir los converters que hemos tenido que hacer en las versiones anteriores, en la MSDN vienen unos cuantos ejemplos los podéis descargar y echar un vistazo, yo como siempre os lo voy a enseñar a través de un ejemplo.

Si creamos la típica clase Persona con las siguientes propiedades

1 public enum Sexo 2 { 3 hombre, 4 mujer 5 } 6 7 public class Person : INotifyPropertyChanged 8 { 9 #region Properties 10 11 public string Nombre 12 { 13 get { return _nombre; } 14 set { _nombre = value; OnPropertyChanged("Nombre"); } 15 } 16 17 private string _nombre; 18 19 public string Apellido 20 { 21 get { return _apellido; } 22 set { _apellido = value; OnPropertyChanged("Apellido"); } 23 } 24 private string _apellido; 25 26 public int Edad 27 { 28 get { return _edad; } 29 set { _edad = value; OnPropertyChanged("Edad"); } 30 } 31 private int _edad; 32 33 public DateTime FechaNacimiento 34 { 35 get { return _fechaNacimiento; } 36 set { _fechaNacimiento = value; OnPropertyChanged("FechaNacimiento"); } 37 } 38 private DateTime _fechaNacimiento; 39 40 public Sexo Sex 41 { 42 get { return _sex; } 43 set { _sex = value; OnPropertyChanged("Sex"); } 44 } 45 private Sexo _sex; 46 47 #endregion 48 49 #region Constructor(s) 50 51 public Person() { } 52 53 public Person(string firstName, string lastName, int age, DateTime birthDate, Sexo sex) 54 { 55 this.Nombre = firstName; 56 this.Apellido = lastName; 57 this.Edad = age; 58 this.FechaNacimiento = birthDate; 59 this.Sex = sex; 60 } 61 62 #endregion 63 64 #region INotifyPropertyChanged Members 65 66 public event PropertyChangedEventHandler PropertyChanged; 67 68 void OnPropertyChanged(string propName) 69 { 70 if (this.PropertyChanged != null) 71 this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); 72 } 73 74 #endregion 75 }

 

Típica clase que al hacer el binding deberíamos realizar diferentes converters para el Sexo, la Edad y el Nombre, con BindingBase.StringFormat vamos a ver como no harían falta estos converters

En el code-behind de la ventana crearíamos la instancia de persona y asignaríamos el DataContext

1 public partial class MainWindow : Window 2 { 3 public Person Person 4 { 5 get 6 { 7 if (_person == null) 8 _person = new Person("Oscar", "Alvarez", 29, new DateTime(1978, 3, 9), Sexo.hombre); 9 10 return _person; 11 } 12 set 13 { 14 _person = value; 15 } 16 }private Person _person; 17 18 public MainWindow() 19 { 20 InitializeComponent(); 21 this.DataContext = Person; 22 } 23 24 }

Ahora el Xaml para ver como utilizaríamos la propiedad

1 <Window x:Class="WPFStringFormat.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid> 6 <StackPanel> 7 <TextBlock Margin="5"> 8 <TextBlock.Text> 9 <MultiBinding StringFormat="Hola {0} {1}!"> 10 <Binding Path="Nombre"/> 11 <Binding Path="Apellido"/> 12 </MultiBinding> 13 </TextBlock.Text> 14 </TextBlock> 15 <TextBlock Text="{Binding Path=Edad, StringFormat='Tienes{0} años.'}" Margin="5"/> 16 <TextBlock Text="{Binding Path=Sex, StringFormat='Eres un {0}.'}" Margin="5"/> 17 <TextBlock Text="{Binding Path=FechaNacimiento, StringFormat='naciste en {0:dd/MM/yyyy}'}" Margin="5"/> 18 </StackPanel> 19 </Grid> 20 </Window> 21

Con esta nueva característica ahorraremos bastantes converters

Deja un comentario

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