Friday, October 12, 2012

Silverlight, TextBlock inside Grid

Silverlight, TextBlock inside Grid

I'm totally new to silverlight and I was given a task to modify it. My problem is very simple (if done in asp.net webforms). Basically, in the grid, i want to append year to something like this.

Jan + "-" + DateTime.Now.Year.ToString()  Feb + "-" + DateTime.Now.Year.ToString() 

etc..etc..

The xaml looks like this

<Grid x:Name="ContentGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">                 <Grid.Resources>  <DataTemplate x:Key="mykey1">                         <Grid >....</Grid> </DataTemplate> <DataTemplate x:Key="mykey2">                         <Grid >....</Grid> </DataTemplate> <DataTemplate x:Key="mykey3">                         <Grid > <StackPanel Orientation="Vertical"> <Border BorderBrush="{StaticResource LogicaPebbleBlackBrush}" BorderThickness="1"> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal" Style="{StaticResource HeaderStackPanel}">   <TextBlock Style="{StaticResource HeaderTextBlock}" Text="Jan-2013" Width="75" TextAlignment="Center"/> <TextBlock Style="{StaticResource HeaderTextBlock}" Text="Feb-2013" Width="75" TextAlignment="Center"/>   </StackPanel> </StackPanel> </Grid> </DataTemplate> </ Grid> </DataTemplate> 

I just want to make the year dynamic so it changes yearly. Please help.

Answers & Comments...

Answer: 1

I don't know if this can be done directly in XAML.

This is best done using binding. In Silverlight, you bind most data sources to a property in code-behind (i.e. ViewModel).

In a nutshell:

  1. Set DataContext of page to a code-behind class (typically a ViewModel)
  2. ViewModel must implement INotifyPropertyChanged interface
  3. Bind the text of your TextBox to use a Date property in the ViewModel that does the calculation in code

Once you have your DataContext setup, you can write XAML as follows:

<TextBlock Text="{Binding Path=Year, Mode=OneWay}" /> 

Your ViewModel property will look as follows:

public class ViewModel : INotifyPropertyChanged {     private DateTime _year = DateTime.Now;     public DateTime Year     {         get { return _year; }    // <--- append whatever here or in the setter         set         {             _year = value;               if( this.PropertyChanged != null )             {                 this.PropertyChanged( this, new PropertyChangedEventArgs( "Year" ) );             }          }      }   ... } 
by : Jordan Parmerhttp://stackoverflow.com/users/20133

Answer: 2

This may help you. It's xaml only:

<Grid.Resources>       <System:DateTime  x:Key="DateTimeDataSource"/> </Grid.Resources>  <TextBlock DataContext="{Binding Source={StaticResource DateTimeDataSource}}"        Text="{Binding Today.Year}"> </TextBlock> 

Make sure you add this namespace:

xmlns:System="clr-namespace:System;assembly=mscorlib"  

You can display other DateTime properties too: Now.Day, Today.Month, etc.

by : Big Daddyhttp://stackoverflow.com/users/1240933




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog