嗨,我是Windows Phone新手。我正在开发一个应用程序,其中有一个日历,其中包含来自数据库(dyanmic)的多个事件。对于日历,我使用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;
}
}
}
}
}
请帮帮我... Thanx提前
答案 0 :(得分:1)
我认为问题在于您的转换器。您只是在比较时改变了一天的颜色if(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;
}
答案 1 :(得分: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;
}