// Custom class implements the IValueConverter interface.
public class DateToStringConverter : IValueConverter
{
#region IValueConverter Members
// Define the Convert method to change a DateTime object to a month string.
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// value is the data from the source object.
DateTime thisdate = (DateTime)value;
int monthnum = thisdate.Month;
string month;
switch (monthnum)
{
case 1:
month = "January";
break;
case 2:
month = "February";
break;
default:
month = "Month not found";
break;
}
// Return the value to pass to the target.
return month;
}
// ConvertBack is not implemented for a OneWay binding.
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
사용법
- <Grid.Resources>
- <DataTemplate x:Name="dataTemplate">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition />
- <ColumnDefinition />
- </Grid.ColumnDefinitions>
- <TextBlock Grid.Column="0" Text="{Binding Month, Converter={StaticResource Converter1}}"/>
- <TextBlock Grid.Column="1" Text="{Binding Total}"/>
- </Grid>
- </DataTemplate>
- </Grid.Resources>
- <ItemsControl x:Name="IC1" ItemsSource="{Binding}" ItemTemplate="{StaticResource dataTemplate}"/>
'WPF' 카테고리의 다른 글
| 초간단 실버라이트 웹서비스 사용하기 (0) | 2009.06.30 |
|---|---|
| 컨테이너(Container) (0) | 2009.06.03 |
| 모듈화 (0) | 2009.06.03 |
| XML , RSS 기사받아오기 및 웹서비스로 다시 보내주기 (0) | 2009.05.29 |
| 실버라이트 초간단 ADO.NET Entity Data Model 만들기 (0) | 2009.05.29 |
| INotifyPropertyChanged 변경알림 (0) | 2009.05.28 |
| 델리게이트(delegate)로 유저컨트롤 이벤트 케치하기 (0) | 2009.05.28 |
| 초간단 컬렉션에 바인딩하고 마스터/세부 뷰 만들기 (0) | 2009.05.28 |
| Silverlight SDK Samples (0) | 2009.05.28 |
| INotifyPropertyChanged (0) | 2009.05.28 |