Articles → Silverlight → Ivalueconverter In Silverlight
Ivalueconverter In Silverlight
Software Requirement
- Visual studio 2008(or higher version) is installed on your machine
- Silverlight (4 or 5) runtime is installed on your machine
- Silverlight plug-in is installed on your machine
Prerequisite Knowledge
- How to create a Silverlight application?
- What is MVVM pattern?
- What is a XAML file?
- How to bind data in XAML file?
- What is the difference between one way and two way binding?
Binding Source Vs Binding Target
Ivalueconverter Interface
Convert Method
public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
Convertback Method
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
Convertparameters Attribute
Creation Of Date Convertor
- Create a class that implements IValueConvertor interface.
- Add methods Convert() and ConvertBack().
- Add logic for date conversion in both the methods.
public class DateConvertor: IValueConverter {
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture) {
object returnValue = new object();
if (value != null) {
returnValue = String.Format("{0:ddd, MMM d, yyyy}", value);
} else {
returnValue = String.Format("{0:ddd, MMM d, yyyy}", DateTime.Now);
}
return returnValue;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture) {
object returnValue = new object();
if (value != null) {
DateTime dt = DateTime.MinValue;
if (DateTime.TryParse(value.ToString(), out dt)) {
returnValue = String.Format("{0:ddd, MMM d, yyyy}", value);
} else {
returnValue = String.Format("{0:ddd, MMM d, yyyy}", DateTime.Now);
}
} else {
returnValue = String.Format("{0:ddd, MMM d, yyyy}", DateTime.Now);
}
return returnValue;
}
}
View Creation
<UserControl x:Class="ConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyConvertorClass">
<UserControl.Resources>
<local:ConverterSampleViewModel x:Key="viewmodel"></local:ConverterSampleViewModel>
<local:DateConvertor x:Key="MyConvertor"></local:DateConvertor>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource viewmodel}}">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Content="Click Me" Grid.Row="1" Command="{Binding ClickMeButtonCommand}" Height="50" Width="100"/>
<TextBox Width="200" Height="50"
Text="{Binding DateFormatProp,Converter={StaticResource MyConvertor},Mode=TwoWay}"/>
</Grid>
</UserControl>
Text="{Binding DateFormatProp,Converter={StaticResource MyConvertor}
Viewmodel Creation
- DateFormatProp – Gets and sets the current system date
- ClickMeButtonCommand – Invokes click event of the button
Public class ConverterSampleViewModel: ViewModelBase {
private DateTime _DateFormatProp;
private readonly ICommand _clickMeButtonCommand;
public ConverterSampleViewModel() {
this._clickMeButtonCommand = new DelegateCommand(this.ClickMeButtonClickEvent);
}
public void ClickMeButtonClickEvent() {
// Add a time
DateFormatProp = DateTime.Now;
}
public DateTime DateFormatProp {
get {
return _DateFormatProp;
}
set {
_DateFormatProp = value;
OnPropertyChanged("DateFormatProp");
}
}
public ICommand ClickMeButtonCommand {
get {
return this._clickMeButtonCommand;
}
}
}
Output
Click to Enlarge