如何在Windows手机日历的日期中放置一个点



我正在开发一个Windows Phone应用程序,在这里我从.net Web服务获取XML数据。 web方法返回日期列表,只有这些日期我需要通过放置点或颜色在日历上突出显示,除此之外,我可以将所选日期作为输入传递给另一个Web方法,但无法同时突出显示日期列表。 下面是我尝试的代码。

XElement xmlNews = XElement.Parse(e.Result.ToString());

        Cal.DataContext = from item in xmlNews.Descendants("Events")
                           select new Institute()
                           {
                               Adress = item.Element("Event_From").Value
                           };
<wpControls:Calendar 
            x:Name="Cal" MonthChanged="Cal_MonthChanged"  DatesSource="{Binding}" SelectedDate="{Binding Adress}"
            MonthChanging="Cal_MonthChanging"
            SelectionChanged="Cal_SelectionChanged"
            DateClicked="Cal_DateClicked"
            EnableGestures="True" Margin="2,-5,0,21" /> 

我的猜测是,您使用的是 Codeplex 中的 Windows Phone Controls 7 库中提供的日历。要根据需要自定义日历,您需要实现一个转换器并使其实现 IDateToBrushConverter。此接口提供单个方法转换,日历将调用该方法,询问要使用哪个画笔作为指定日历案例的背景或前景。这里的诀窍是,您指定转换器的全局实例并为其提供从后端 Web 服务返回的日期,例如将其注入构造函数或将其指定为静态方法(我不喜欢),然后在您的 Convert 方法中,您检查日历案例的日期是否在指定的列表中, 如果是这样,您可以根据需要退回所需的刷子下面是一个示例:

public class CalendarColorBackgroundConverter : IDateToBrushConverter 
{
    // ctor
    public CalendarColorBackgroundConverter(IEnumerable<DateTime> datesToHighlight)
    {
         this.DatesToHighlight = datesToHighlight;
    }
    IEnumerable<DateTime> DatesToHighlight {get; private set;}
    // some predefined brushes.
    //static SolidColorBrush transparentBrush = new SolidColorBrush(Colors.Transparent);
    static SolidColorBrush whiteColorBrush = new SolidColorBrush(Colors.White);
    static SolidColorBrush selectedColorBrush = new SolidColorBrush(Colors.DarkGray);
    static SolidColorBrush todayColorBrush = new SolidColorBrush(Color.FromArgb(255, 74, 128, 1));
    static SolidColorBrush Foreground = new SolidColorBrush(Color.FromArgb(255, 108, 180, 195));
    public Brush Convert(DateTime currentCalendarCaseDate, bool isSelected, Brush defaultValue, BrushType brushType)
    {
        var highlightDate = this.DatesToHighlight.Any(d => d.Year == currentCalendarCaseDate.Year && d.Month == currentCalendarCaseDateMonth.Month &&
                      d.Day == currentCalendarCaseDate.Day);
        if (brushType == BrushType.Background) 
        {
            if (highlightDate) // background brush color when the calendar case is selected
                return todayColorBrush;
            else
                return whiteColorBrush;
        } 
        else
        {
            if (highlightDate)
                return whiteColorBrush;
            return Foreground;
        } 
    } 
}

希望这有帮助。

最新更新