Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
210
XamMonthCalendar CalendarDayStyleSelector StyleSelector not fired
posted

Hey guys,

I have a problem with the CalendarDayStyleSelector of a XamMonthCalendar.

I looked into the example and tried to build a same function as there.

My problem is, that I not get the StyleSelector event fired even once.

I want to let holiday days have a different color and the ability, to repaint these days, too. Because a user can double click a day and change its state from a normal day to holiday or the other way.

Because my problem with the StyleSelector I tried the LoadedEvent of the CalendarDays but not get them to reload if the Data changes.

I completely not get, why the StyleSelector not fires.

Here are my code samples:

<metro:MetroWindow x:Class="Dispo.Betriebskalender"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:metro="http://metro.mahapps.com/winfx/xaml/controls"
    xmlns:igWindows="http://infragistics.com/Windows"
    xmlns:igThemes="http://infragistics.com/Themes"
    xmlns:dispo="clr-namespace:Dispo"
    xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
    xmlns:wpf="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:ig="http://schemas.infragistics.com/xaml"
    xmlns:igPrim="http://schemas.infragistics.com/xaml/primitives"
    xmlns:igDP="http://schemas.infragistics.com/xaml/wpf"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:Dispo"
    xmlns:igEditors="http://infragistics.com/Editors"
    WindowTransitionsEnabled="False"
    TextElement.Foreground="{DynamicResource MaterialDesignBody}"
    Background="{DynamicResource MaterialDesignPaper}"
    TextElement.FontWeight="Medium"
    TextElement.FontSize="15"
    Style="{StaticResource Roboto}"
    Title="Betriebskalender"
    WindowStartupLocation="CenterOwner"
    Closing="MetroWindow_Closing"
    >
    <metro:MetroWindow.TitleTemplate>
        <DataTemplate>
            <TextBlock 
                Text="Betriebskalender"
                TextTrimming="CharacterEllipsis"
                VerticalAlignment="Center"
                Margin="12 -1 8 0"
                FontWeight="Regular"
                FontSize="18"
                />
        </DataTemplate>
    </metro:MetroWindow.TitleTemplate>
    <metro:MetroWindow.Resources>
        <ResourceDictionary>
            <Style x:Key="Feiertag"
                   TargetType="{x:Type igEditors:CalendarDay}">
                <Setter Property="Background" Value="Red">
                </Setter>
            </Style>
        </ResourceDictionary>
    </metro:MetroWindow.Resources>
    <Grid
        >
        <Grid.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <igThemes:ResourceWasher 
                        WashMode="HueSaturationReplacement" 
                        WashColor="#4CAF50" 
                        AutoWash="True"
                        >
                        <igThemes:ResourceWasher.SourceDictionary>
                            <ResourceDictionary>
                                <ResourceDictionary.MergedDictionaries>
                                    <igThemes:EditorsMetro/>
                                </ResourceDictionary.MergedDictionaries>
                            </ResourceDictionary>
                        </igThemes:ResourceWasher.SourceDictionary>
                    </igThemes:ResourceWasher>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="100*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100*"/>
        </Grid.ColumnDefinitions>
        <igDP:XamMonthCalendar
            Name="xcCalendar"
            Grid.Row="0"
            Grid.Column="0"
            CalendarDimensions="4,4"
            TodayButtonVisibility="Collapsed"
            CalendarDayStyleSelector="{Binding Path=DayStyleSelector, RelativeSource={RelativeSource AncestorType=Page}}"
            >
            
        </igDP:XamMonthCalendar>
    </Grid>
</metro:MetroWindow>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Infragistics.Windows.Editors;
using MaterialDesignThemes.Wpf;

namespace Dispo
{
    /// <summary>
    /// Interaktionslogik für Betriebskalender.xaml
    /// </summary>
    public partial class Betriebskalender
    {

        public bool Shown = false;

        clsDaten Daten = new clsDaten();

        public List<DateTime> DatenFeiertage { get; set; }
        public List<DateTime> DatenArbeitsfreieTage { get; set; }

        public List<DateTime> selDates;

        private StyleSelector _dayStyleSelector;

        public Betriebskalender()
        {
            this._dayStyleSelector = new CalendarDayStyleSelector(this);

            InitializeComponent();

            DatenLaden();

        }

        public StyleSelector DayStyleSelector
        {
            get { return this._dayStyleSelector; }
        }

        public void DatenLaden()
        {

            DateTime tempStartdatum = DateTime.Today;
            DateTime tempEnddatum = DateTime.Today.AddYears(2);

            xcCalendar.MinDate = tempStartdatum;
            xcCalendar.MaxDate = tempEnddatum;

            DataTable dtFeiertage = Daten.GibFeiertage(tempStartdatum, tempEnddatum);
            DataTable dtArbeitsfreieTage = Daten.GibArbeitsfreieTage(tempStartdatum, tempEnddatum);

            DatenFeiertage = dtFeiertage.Rows.OfType<DataRow>().Select(dr => dr.Field<DateTime>("Datum")).ToList();
            DatenArbeitsfreieTage = dtArbeitsfreieTage.Rows.OfType<DataRow>().Select(dr => dr.Field<DateTime>("Datum")).ToList();

        }

        public void Aktualisieren()
        {

            DateTime tempStartdatum = DateTime.Today;
            DateTime tempEnddatum = DateTime.Today.AddYears(2);

            DataTable dtFeiertage = Daten.GibFeiertage(tempStartdatum, tempEnddatum);
            DataTable dtArbeitsfreieTage = Daten.GibArbeitsfreieTage(tempStartdatum, tempEnddatum);

            DatenFeiertage = dtFeiertage.Rows.OfType<DataRow>().Select(dr => dr.Field<DateTime>("Datum")).ToList();
            DatenArbeitsfreieTage = dtArbeitsfreieTage.Rows.OfType<DataRow>().Select(dr => dr.Field<DateTime>("Datum")).ToList();

        }




        private void MetroWindow_Closing(object sender, CancelEventArgs e)
        {
            Shown = false;
        }

    }
    public class CalendarDayStyleSelector : StyleSelector
    {
        private Betriebskalender _page;

        internal CalendarDayStyleSelector(Betriebskalender page)
        {
            this._page = page;
        }

        public override System.Windows.Style SelectStyle(object item, DependencyObject container)
        {
            CalendarDay day = (CalendarDay)container;
            DateTime date = day.StartDate;

            if (date.Month == 12 && date.Day == 25)
                return (System.Windows.Style)this._page.Resources["Feiertag"];


            return null;
        }
    }
}