In a previous article, how does C# code behind interact with an XAML interface within a Silverlight 2 context? - Data Validation: Part VI we demonstrate how to implement validation logic within a Silverlight 2 application and XAML UI and how a binding engine provides interaction between the two sides, namely the source object and the XAML UI side so that the final user will be notified when invalid entries are used. In this article I will answer the fifth question posed in the article how does C# code behind interact with an XAML interface within a Silverlight 2 context? - Introduction: Part I, which is: what if there is a need to display data in format that differs to the original one?
The solution is brought through the present article. Consider this class Person representation:
public class Person : INotifyPropertyChanged
{
public Person() { }
private string _FirstName;
public string FirstName{
get { return _FirstName; }
set
{
_FirstName = value;
if (PropertyChanged != null){
PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
}
private string _LastName;
public string LastName
{
get { return _LastName; }
set{
_LastName = value;
if (PropertyChanged != null){
PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
}
}
}
private DateTime _BirthDate;
public DateTime BirthDate
{
get { return _BirthDate; }
set{
_BirthDate = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));
}
}
}
private string _Gender;
public string Gender{
get { return _Gender; }
set{
_Gender = value;
if (PropertyChanged != null){
PropertyChanged(this, new PropertyChangedEventArgs("Gender"));
}
}
}
public override string ToString(){
return string.Format("First name: {0},last name: {1},age: {2},gender: {3}",
FirstName,
LastName,
BirthDate,
Gender);
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
Then consider this simple XAML presentation
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="Silverlight.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="550" Height="50">
<StackPanel x:Name="LayoutRoot"
Margin="10"
Orientation="Vertical"
Background="White"
>
<TextBlock x:Name="txtPerson"
FontSize="12"
FontWeight="Bold"
Foreground="Black"
Text="First name: Bejaoui,last name: Bechir ,Birth day: 11/04/78 ,gender: Male"
/>


Join the conversation! Your thoughts help the community grow.