突出显示日历中的当前日期不起作用

时间:2015-07-27 09:18:37

标签: c# xaml windows-phone-8.1

我想突出显示日历的日期。日期会添加到TextBox。 以下是示例代码。我无法突出显示当前日期。对此有任何帮助

XAML:

<StackPanel HorizontalAlignment="Center" Background="#FF575757">
    <StackPanel>
        <Grid>
            <Button Content="-" BorderBrush="{x:Null}" HorizontalAlignment="Left" Click="previousMonth"/>
            <TextBlock x:Name="CalendarHeader" Text="August 2014" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="ExtraLight" FontSize="26"/>
            <Button Content="+" BorderBrush="{x:Null}" HorizontalAlignment="Right" Click="nextMonth"/>
        </Grid>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Grid Width="50" Height="50" Background="#FF363636">
            <TextBlock Text="S" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
        <Grid Width="50" Height="50" Background="#FF363636">
            <TextBlock Text="M" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
        <Grid Width="50" Height="50" Background="#FF363636">
            <TextBlock Text="T" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
        <Grid Width="50" Height="50" Background="#FF363636">
            <TextBlock Text="W" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
        <Grid Width="50" Height="50" Background="#FF363636">
            <TextBlock Text="T" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
        <Grid Width="50" Height="50" Background="#FF363636">
            <TextBlock Text="F" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
        <Grid Width="50" Height="50" Background="#FF363636">
            <TextBlock Text="S" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
    </StackPanel>
    <StackPanel x:Name="Calendar" Margin="0, 0, 0, 5">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Grid x:Name="sundayGrid" Width="50" Height="30">

                <TextBlock  Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>

            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <Grid Width="50" Height="30">
                <TextBlock Text="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </StackPanel>
    </StackPanel>
</StackPanel>

我试图突出显示当前日期 xaml.cs:

public void Initialize_Calendar(DateTime date)
{

    CalendarHeader.Text = date.ToString("MMMM yyyy");
    date = new DateTime(date.Year, date.Month, 1);
    int dayOfWeek = (int)date.DayOfWeek + 1;
    int daysOfMonth = DateTime.DaysInMonth(date.Year, date.Month);
    int i = 1;
    foreach (var o1 in Calendar.Children)
    {
        foreach (var o2 in (o1 as StackPanel).Children)
        {
            var o3 = (o2 as Grid).Children[0] as TextBlock;
            if (i >= dayOfWeek && i < (daysOfMonth + dayOfWeek))
                o3.Text = (i - dayOfWeek + 1).ToString();
            else
                o3.Text = "";
            i++;
            if (calendarDate.Date.Equals(DateTime.Now.Date))
            {
                o3.Foreground = new SolidColorBrush(Colors.Blue);
            }
        }
    }

DateTime calendarDate;
public MainPage()
{
    this.InitializeComponent();

    this.NavigationCacheMode = NavigationCacheMode.Required;

    calendarDate = DateTime.Today;
    Initialize_Calendar(calendarDate);
}

0 个答案:

没有答案