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.
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:
- Set DataContext of page to a code-behind class (typically a ViewModel)
- ViewModel must implement INotifyPropertyChanged interface
- 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/20133Answer: 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/1240933Answer: 3
You can do this with celltemplates
<sdk:DataGrid AutoGenerateColumns="False" Height="175" HorizontalAlignment="Left" Margin="72,58,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="241" > <sdk:DataGrid.Columns> <sdk:DataGridTemplateColumn> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Month}"></TextBlock> <TextBlock Text="-"/> <TextBlock Text="{Binding DayNow}"/> </StackPanel> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> </sdk:DataGridTemplateColumn> </sdk:DataGrid.Columns> </sdk:DataGrid>
Where your itemssource would be a list of your class
for example
Public Class MyClassName { public string Month{get;set;} //the name of thy month public int DateNow{get; set;} //this would be your year } New List<MyClassName>() cs = new List<MyClassName>(); int yr= datetime.Now.Year; cs.Add("jan", yr); cs.Add("feb", yr); cs.Add(...etc... datagrid1.ItemsSource= null; datagrid1.ItemsSource = cs;
This solution makes use of the standard binding concepts in datagrid.
I hope this is the solution for your need.
by : Schuerehttp://stackoverflow.com/users/1100230
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog