silverlight 中用 telerik RadDatePicker 自定义显示周期的DatePicker控件
最近做项目客户需要一个按某种周期显示日期的控件 这样方便客户按周期选择日期(有点像大姨妈周期表)。查了一下相关资料发现在ASP.net中RadDatePicker自带<CalendarDayTemplates>,<telerik:DayTemplate>和<SpecialDays>而silverlight中并没有相关的templete。所以没办法自己研究了一下 发现只能在style里面做。所以就在style中加了一个datetemplete
style:
<DataTemplate x:Key="DataTemplate1">
<Grid>
<Telerik_Windows_Controls_Calendar:CalendarView Height="18" HeaderVisibility="Collapsed" HideColumn="True" HideRow="True" Width="22" HorizontalAlignment="Center" Background="{Binding Path=DataContext,ElementName=dayCell,Converter={StaticResource DayCellBackgroundConvert},Mode=TwoWay}"></Telerik_Windows_Controls_Calendar:CalendarView>
<TextBlock x:Name="dayCell" FontSize="10" HorizontalAlignment="Center" DataContext="{Binding}" Text="{Binding }" VerticalAlignment="Center" Margin="1,0,0,0"/>
</Grid>
</DataTemplate>
usercontrol:
<Grid x:Name="LayoutRoot" Background="White"> <telerik:RadDatePicker Height="Auto" Width="Auto" MinWidth="90" x:Name="datepicker" SelectionChanged="datepicker_SelectionChanged" HorizontalAlignment="Left" CalendarStyle="{StaticResource RadCalendarStyle1}"> </telerik:RadDatePicker> <TextBox x:Name="txtHeaderstr" Visibility="Collapsed" TextChanged="Headerstr_TextChanged" Text="{Binding Path=Text,ElementName=ForegroundText,Mode=TwoWay}"></TextBox> </Grid>
其中CalendarView是控制背景色的而TextBlock则是其中显示的日期DayCellBackgroundConvert是根据日期在转换背景色用的。 但是由于每一个cell我们都需要调用这个convert 所以用singleton class来存贮一下数据库中的周期数据和控件需要的数据 以便于提高性能。
public class Singleton { private static volatile Singleton _instance = null; private static object objLock = new Object(); private Singleton() { } public static Singleton Instance { get { if (_instance == null) { lock (objLock) { if (_instance == null) { _instance = new Singleton(); } } } return _instance; } } public List<ClientSpecificAdhocCalenderBE> IClientSpecificAdhocCalender { get; set; } public MarketBE currentMarket { get; set; } public string HeaderStr { get; set; } }
下面是convert:
public class DayCellBackgroundConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { //green Color Tcol = new Color(); Tcol.A = 100; Tcol.B = 0; Tcol.G = 128; Tcol.R = 0; //yellow Color Fcol = new Color(); Fcol.A = 100; Fcol.B = 0; Fcol.G = 255; Fcol.R = 255; if (value != null) { var currentcell = value as Telerik.Windows.Controls.Calendar.CalendarButtonContent; int v; bool ok = int.TryParse(currentcell.Text.ToString(), out v); if (ok) { if (currentcell.ButtonType == Telerik.Windows.Controls.Calendar.CalendarButtonType.Date) { //var cellowner = currentcell.Owner as System.Windows.Controls.Control; int i = 0; var rowNumItems = Singleton.Instance.IClientSpecificAdhocCalender .OrderBy(o => o.CycleStart).Select(p => new { RowIndex = ++i, Item = p }); var item = rowNumItems.Where(x => x.Item.CycleStart <= currentcell.Date && x.Item.CycleEnd >= currentcell.Date).FirstOrDefault(); if(item!=null) { int row = item.RowIndex; if (row % 2 == 0) { return new SolidColorBrush(Tcol); } else { return new SolidColorBrush(Fcol); } } } } } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Color Tcol = new Color(); Tcol.A = 100; Tcol.B = 0; Tcol.G = 128; Tcol.R = 0; return ((SolidColorBrush)value) == new SolidColorBrush(Tcol); } }
其中 bool ok = int.TryParse(currentcell.Text.ToString(), out v); 这句是用来判断cell中是数字还是字母的 当然这个完全是多余的 只是为了联系所以自己加上去的 完全可以用currentcell.ButtonType来判断。
最终结果是
也不知道说没说明白。第一次发虽然菜了点,还是希望不要被骂吧。 呵呵。
posted on 2013-07-01 17:07 Lucifer_Tian 阅读(767) 评论(0) 收藏 举报
浙公网安备 33010602011771号