duminică, 15 ianuarie 2012

Converters with Parameters


Converter


[ValueConversion(typeof (object), typeof (string))]
public class StringFormatConverter : IValueConverter {
  public object Convert(object i_value, Type i_targetType, object i_parameter,                              System.Globalization.CultureInfo i_culture) {
    string _format = i_parameter as string;
    if (!string.IsNullOrEmpty(format)) {
         return string.Format(i_culture, _format, i_value);
        }
    return i_value.ToString();
  }
}


XAML

<Window.Resources>
  <HierarchicalData:Employee x:Key="myEmployee" FirstName="George" LastName="Lache" Position="DEV" Salary="2000"></HierarchicalData:Employee>
  <Converter:StringFormatConverter x:Key="StringFormatConverter" />
</Window.Resources>
<StackPanel DataContext="{StaticResource myEmployee}">
  <Label Content="{Binding FirstName, Converter={StaticResource StringFormatConverter}, ConverterParameter='Many thanks to {0}'}" />
  <Label Content="{Binding Salary, Converter={StaticResource StringFormatConverter}, ConverterParameter='My salary is thanks to {0:c}'}" />
  <Label Content="{Binding Salary, Converter={StaticResource StringFormatConverter}, ConverterParameter=\{0:c\}}" />
  <TextBox Text="{Binding Path=Salary, StringFormat=\{0:c\}}" />
</StackPanel>




Result



Niciun comentariu:

Trimiteți un comentariu