1

嗨,我是 Windows Phone 的新手。我正在开发一个应用程序,其中有一个包含来自数据库(动态)的多个事件的日历。对于日历,我使用 wpcontrols:calendar 并更改我使用 ColorConvertor 的事件的背景颜色。我可以更改一天的背景颜色,但无法更改更多一天的颜色。要求是事件可能在任何时间点发生变化。下面是我的代码:

XAML 代码:

<phone:PhoneApplicationPage
x:Class="cal.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wpControls="clr-namespace:WPControls;assembly=WPControls"
xmlns:WpControlsExample="clr-namespace:cal"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
Loaded="PhoneApplicationPage_Loaded">
<phone:PhoneApplicationPage.Resources>
    <WpControlsExample:ColorConverter x:Key="ColorConverter"/>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
        <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <wpControls:Calendar 
            x:Name="Cal"
            ColorConverter="{StaticResource ColorConverter}"
            MonthChanged="Cal_MonthChanged"
            MonthChanging="Cal_MonthChanging"
            SelectionChanged="Cal_SelectionChanged"
            DateClicked="Cal_DateClicked"
            EnableGestures="True"
            />
        <!--<wpControls:Calendar 
            x:Name="Cal"/>-->
        <Button Grid.Row="1" Content="{Binding ElementName=Cal, Path=SelectedDate}"/>
    </Grid>

</Grid>

CS代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using cal.Resources;
using System.Windows.Media;
using WPControls;
using System.Windows.Media.Imaging;

namespace cal
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.Cal.SelectedDate = DateTime.Today;

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Cal_MonthChanged(object sender, WPControls.MonthChangedEventArgs e)
        {
            MessageBox.Show("Cal_MonthChanged fired.  New year is " + e.Year.ToString() + " new month is " + e.Month.ToString());

        }

        private void Cal_MonthChanging(object sender, WPControls.MonthChangedEventArgs e)
        {
            MessageBox.Show("Cal_MonthChanging fired.  New year is " + e.Year.ToString() + " new month is " + e.Month.ToString());
        }

        private void Cal_SelectionChanged(object sender, WPControls.SelectionChangedEventArgs e)
        {
            MessageBox.Show("Cal_SelectionChanged fired.  New date is " + e.SelectedDate.ToString());
        }

        private void Cal_DateClicked(object sender, WPControls.SelectionChangedEventArgs e)
        {
            MessageBox.Show("Cal_DateClicked fired.  New date is " + e.SelectedDate.ToString());
        }
    }
}

ColorConverter.cs 的 CS 代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using WPControls;
using System.Windows.Media.Imaging;

namespace cal
{
    public class ColorConverter : IDateToBrushConverter
        {
            public Brush Convert(DateTime dateTime, bool isSelected, Brush defaultValue, BrushType brushType)
                {
                    String MyString;
                    MyString = "2013-09-01 21:34 PM";
                    DateTime MyDateTime;
                    MyDateTime = new DateTime();
                    MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",null);
                    if (brushType == BrushType.Background)
                        {
                            if (dateTime == new DateTime(MyDateTime.Year, MyDateTime.Month, MyDateTime.Day))
                                {

                                    // Fill rectangle with an ImageBrush
                                    //blueRectangle.Fill = imgBrush;
                                    return new SolidColorBrush(Colors.Yellow);
                                }
                            else
                                {
                                    return defaultValue;
                                }
                        }
                    else
                        {
                            if (dateTime == new DateTime(DateTime.Today.Year, DateTime.Today.Month, 6))
                                {
                                     return new SolidColorBrush(Colors.Orange);
                                }
                            else
                                {
                                    return defaultValue;
                                }
                        }
                }
        }
}

请帮帮我......提前谢谢

4

2 回答 2

1

这就是您正在寻找的内容。X

public class ColorConverter : IDateToBrushConverter
{
    public IEnumerable<DateTime> Dates { get; set; }

    public Brush Convert(DateTime dateTime, bool isSelected, Brush defaultValue, BrushType brushType)
    {
        if (brushType == BrushType.Background)
        {
            if (Dates != null && Dates.Any(d => d == dateTime))
            {
                return new SolidColorBrush(Colors.Yellow);
            }
            return defaultValue;
        }
        if (dateTime == new DateTime(DateTime.Today.Year, DateTime.Today.Month, 6))
        {
            return new SolidColorBrush(Colors.Orange);
        }
        return defaultValue;
    }
}

}

您可以通过多种方式将日期提供给转换器。这是页面代码隐藏文件的示例

        // Constructor
    public MainPage()
    {
        InitializeComponent();

        var dates = new DateTime[]
            {
                new DateTime(2013,9,10),
                new DateTime(2013,9,14),
                new DateTime(2013,9,20),
                new DateTime(2013,9,30),
            };
        ((ColorConverter) Resources["ColorConverter"]).Dates = dates;
    }
于 2013-09-10T12:11:27.333 回答
1

我认为问题出在你的转换器上。如果 (dateTime == new DateTime(MyDateTime.Year, MyDateTime.Month, MyDateTime.Day))

例如,如果将此代码更改为以下代码,则所有星期日都将变为黄色。这回答了你的问题了吗?谢谢

谢尔盖

    public Brush Convert(DateTime dateTime, bool isSelected, Brush defaultValue, BrushType brushType)
    {
        if (brushType == BrushType.Background)
        {
            if (dateTime.DayOfWeek == DayOfWeek.Sunday)
            {
                return new SolidColorBrush(Colors.Yellow);
            }
            return defaultValue;
        }
        if (dateTime == new DateTime(DateTime.Today.Year, DateTime.Today.Month, 6))
        {
            return new SolidColorBrush(Colors.Orange);
        }
        return defaultValue;
    }
于 2013-09-08T14:53:18.853 回答