1

こんにちは、私は Windows Phone を初めて使用します。データベース(動的)から来る複数のイベントを持つカレンダーがあるアプリケーションに取り組んでいます。カレンダーには wpcontrols:calendar を使用しており、イベントの背景色を変更するには ColorConvertor を使用しています。1 日の背景色を変更することはできますが、1 日以上の色を変更することはできません。要件は、イベントがいつでも変更される可能性があることです。以下は私のコードです:

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

問題はコンバーターにあると思います。(dateTime == new DateTime(MyDateTime.Year, MyDateTime.Month, MyDateTime.Day)) の場合、比較に基づいて 1 日の色のみを変更しています。

このコードをたとえば次のように変更すると、すべての日曜日が黄色に変わります。これはあなたの質問に答えていますか?ありがとう

セルゲイ

    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 に答える