Add a value converter to a binding using XAML : Data Binding « Data « Silverlight






Add a value converter to a binding using XAML

  

<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'
    xmlns:c="clr-namespace:SilverlightApplication3">
    
<StackPanel>
  <StackPanel.Resources>
    <c:MyData x:Key="myDataSource"/>
    <c:MyConverter x:Key="MyConverterReference"/>
    <Style TargetType="TextBlock">
      <Setter Property="FontSize" Value="15"/>
      <Setter Property="Margin" Value="3"/>
    </Style>
  </StackPanel.Resources>
  
  <StackPanel.DataContext>
    <Binding Source="{StaticResource myDataSource}"/>
  </StackPanel.DataContext>

  <TextBlock Text="Unconverted data:"/>
  <TextBlock Text="{Binding Path=TheDate}"/>
  <TextBlock Text="Converted data:"/>
  <TextBlock Name="myconvertedtext" Foreground="{Binding Path=TheDate,Converter={StaticResource MyConverterReference}}">
    <TextBlock.Text>
      <Binding Path="TheDate" Converter="{StaticResource MyConverterReference}"/>
    </TextBlock.Text>
  </TextBlock>
</StackPanel>
</UserControl>
//File:Window.xaml.cs
using System;
using System.Globalization;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Data;
using System.Windows.Media;


namespace SilverlightApplication3
{
  public class MyConverter : IValueConverter
  {
    public object Convert(object o, Type type,object parameter, CultureInfo culture){
        DateTime date = (DateTime)o;
        switch (type.Name)
        {
            case "String":
                return date.ToString("F", culture);
            default:
                return o;
        }
    }
    public object ConvertBack(object o, Type type,object parameter, CultureInfo culture){
        return null;
    }
  }
  public class MyData: INotifyPropertyChanged{
    private DateTime thedate;
    public MyData(){
      thedate = DateTime.Now;
    }
    public DateTime TheDate
    {
      get{return thedate;}
      set{
        thedate = value;
        OnPropertyChanged("TheDate");
      }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String info)
    {
      if (PropertyChanged !=null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
      }
    }
  }
}

   
    
  








Related examples in the same category

1.Binding Application Data to the UIBinding Application Data to the UI
2.Receiving Change Notifications for Bound Data
3.Validating Input for Bounded Data
4.DataBinding and INotifyPropertyChanged
5.Implementing Data Binding in Silverlight ApplicationsImplementing Data Binding in Silverlight Applications
6.Bind to a collection
7.Without Binding
8.Text Data Binding
9.Bind to an Existing Object InstanceBind to an Existing Object Instance
10.Binding FontFamily / FontSize value for current ControlBinding FontFamily / FontSize value for current Control
11.Binding an Emoticon object to a GridBinding an Emoticon object  to a Grid
12.Binding a collection of Emoticon objects to a ListBox through the ItemsSource propertyBinding a collection of Emoticon objects to a ListBox through the ItemsSource property