Bonjour,
Je cherche actuellement à modifier le style CalendarDayButtonStyle pour une petite application du genre agenda. Les modifications que j'aimerais apporter sont l'ajout de deux fonds de couleurs selon la disponibilité dans la journée. De cette manière, il serait possible de voir directement sur la vue les jours où il reste de la disponibilité (l'image ci-dessous est une illustration du rendu voulu).
Pour faire cela j'ai voulu m'inspirer de l'article suivant (lien). J'arrive à obtenir l'affichage voulu mais j'ai un problème au niveau du rafraîchissement de l'affichage : j'aimerais que celui-ci se fasse automatiquement lors d'une modification d'une des listes de dates contenant les date à mettre dans chacune des deux couleurs.
Voici le code me servant à faire cela: (en fichier joint le projet qui me sert au tests)
Fichier .csLe style est celui de base copié avec les modifications marquées ci-dessous).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Controls; using System.Windows.Data; namespace Calandar_Style { /// <summary> /// Logique d'interaction pour CustomCalendar.xaml /// </summary> public partial class CustomCalendar : UserControl { private int i = 1; public CustomCalendar() { InitializeComponent(); calendar.SelectedDate = DateTime.Now; } private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e) { if (i > 30) { i = 1; OrangeDayConverter.Clear(); RedDayConverter.Clear(); } OrangeDayConverter.AddDate(new DateTime(2014, 11,i)); RedDayConverter.AddDate(new DateTime(2014, 11,i+1)); i += 2; } } public class RedDayConverter : IValueConverter { static List<DateTime> dict = new List<DateTime>(); static RedDayConverter() { dict.Add(new DateTime(2014, 11, 2)); } public static void Clear() { dict.Clear(); } public static void AddDate(DateTime value) { dict.Add(value); } object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return null; DateTime date = (DateTime)value; if (!dict.Contains(((DateTime)value).Date)) return null; return date.ToShortDateString(); } object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new Exception("Non implémenté"); } } public class OrangeDayConverter : IValueConverter { static List<DateTime> dictO = new List<DateTime>(); static OrangeDayConverter() { dictO.Add(new DateTime(2014, 11, 1)); } public static void Clear() { dictO.Clear(); } public static void AddDate(DateTime value) { dictO.Add(value); } object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return null; DateTime date = (DateTime)value; if (!dictO.Contains((DateTime)value)) return null; return date.ToShortDateString(); } object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new Exception("Non implémenté"); } } }
Fichier .xaml (ressources ligne 23 et autres modifications ligne 163)Comment puis je faire pour avoir un rafraîchissement automatique lors de la modification des listes de dates?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 <UserControl x:Class="Calandar_Style.CustomCalendar" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:src="clr-namespace:Calandar_Style" xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" mc:Ignorable="d"> <Grid> <Calendar x:Name="calendar" SelectedDatesChanged="calendar_SelectedDatesChanged"> <Calendar.CalendarDayButtonStyle> <!--Style for the days of a month.--> <Style TargetType="CalendarDayButton"> <Setter Property="MinWidth" Value="5" /> <Setter Property="MinHeight" Value="5" /> <Setter Property="FontSize" Value="10" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="CalendarDayButton"> <ControlTemplate.Resources> <src:RedDayConverter x:Key="RedDay" /> <src:OrangeDayConverter x:Key="OrangeDay"/> <Color x:Key="SelectedBackgroundColor">#FFAFEEEE</Color> <Color x:Key="SelectedUnfocusedColor">#FFDDDDDD</Color> <Color x:Key="ControlMouseOverColor">#FFF0F8FF</Color> <Color x:Key="RedDayColor">#80FF0000</Color> <Color x:Key="OrangeDayColor">#80FF9900</Color> </ControlTemplate.Resources> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="CommonStates"> <VisualStateGroup.Transitions> <VisualTransition GeneratedDuration="0:0:0.1" /> </VisualStateGroup.Transitions> <VisualState Name="Normal" /> <VisualState Name="MouseOver"> <Storyboard> <DoubleAnimation Storyboard.TargetName="HighlightBackground" Storyboard.TargetProperty="Opacity" To="0.5" Duration="0"/> </Storyboard> </VisualState> <VisualState Name="Pressed"> <Storyboard> <DoubleAnimation Storyboard.TargetName="HighlightBackground" Storyboard.TargetProperty="Opacity" To="0.5" Duration="0" /> </Storyboard> </VisualState> <VisualState Name="Disabled"> <Storyboard> <DoubleAnimation Storyboard.TargetName="HighlightBackground" Storyboard.TargetProperty="Opacity" To="0" Duration="0" /> <DoubleAnimation Storyboard.TargetName="NormalText" Storyboard.TargetProperty="Opacity" To=".35" Duration="0" /> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup Name="SelectionStates"> <VisualStateGroup.Transitions> <VisualTransition GeneratedDuration="0" /> </VisualStateGroup.Transitions> <VisualState Name="Unselected" /> <VisualState Name="Selected"> <Storyboard> <DoubleAnimation Storyboard.TargetName="SelectedBackground" Storyboard.TargetProperty="Opacity" To=".75" Duration="0" /> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup Name="CalendarButtonFocusStates"> <VisualStateGroup.Transitions> <VisualTransition GeneratedDuration="0" /> </VisualStateGroup.Transitions> <VisualState Name="CalendarButtonFocused"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DayButtonFocusVisual" Storyboard.TargetProperty="Visibility" Duration="0"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="CalendarButtonUnfocused"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DayButtonFocusVisual" Storyboard.TargetProperty="Visibility" Duration="0"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <Visibility>Collapsed</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup Name="ActiveStates"> <VisualStateGroup.Transitions> <VisualTransition GeneratedDuration="0" /> </VisualStateGroup.Transitions> <VisualState Name="Active" /> <VisualState Name="Inactive"> <Storyboard> <ColorAnimation Duration="0" Storyboard.TargetName="NormalText" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" To="#FF777777" /> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup Name="DayStates"> <VisualStateGroup.Transitions> <VisualTransition GeneratedDuration="0" /> </VisualStateGroup.Transitions> <VisualState Name="RegularDay" /> <VisualState Name="Today"> <Storyboard> <DoubleAnimation Storyboard.TargetName="TodayBackground" Storyboard.TargetProperty="Opacity" To="1" Duration="0" /> <ColorAnimation Duration="0" Storyboard.TargetName="NormalText" Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" To="#FFFFFFFF" /> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup Name="BlackoutDayStates"> <VisualStateGroup.Transitions> <VisualTransition GeneratedDuration="0" /> </VisualStateGroup.Transitions> <VisualState Name="NormalDay" /> <VisualState Name="BlackoutDay"> <Storyboard> <DoubleAnimation Duration="0" Storyboard.TargetName="Blackout" Storyboard.TargetProperty="Opacity" To=".2" /> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Rectangle x:Name="TodayBackground" RadiusX="1" RadiusY="1" Opacity="0"> <Rectangle.Fill> <SolidColorBrush Color="{DynamicResource SelectedBackgroundColor}" /> </Rectangle.Fill> </Rectangle> <Rectangle x:Name="SelectedBackground" RadiusX="1" RadiusY="1" Opacity="0"> <Rectangle.Fill> <SolidColorBrush Color="{DynamicResource SelectedBackgroundColor}" /> </Rectangle.Fill> </Rectangle> <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" /> <Rectangle x:Name="HighlightBackground" RadiusX="1" RadiusY="1" Opacity="0"> <Rectangle.Fill> <SolidColorBrush Color="{DynamicResource ControlMouseOverColor}" /> </Rectangle.Fill> </Rectangle> <ContentPresenter x:Name="NormalText" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="5,1,5,1"> <TextElement.Foreground> <SolidColorBrush Color="#FF333333" /> </TextElement.Foreground> </ContentPresenter> <Path x:Name="Blackout" Opacity="0" Margin="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Fill="#FF000000" Stretch="Fill" Data="M8.1772461,11.029181 L10.433105, 11.029181 L11.700684,12.801641 L12.973633, 11.029181 L15.191895,11.029181 L12.844727, 13.999395 L15.21875,17.060919 L12.962891, 17.060919 L11.673828,15.256231 L10.352539, 17.060919 L8.1396484,17.060919 L10.519043, 14.042364 z" /> <Rectangle x:Name="DayButtonFocusVisual" Visibility="Collapsed" IsHitTestVisible="false" RadiusX="1" RadiusY="1"> <Rectangle.Stroke> <SolidColorBrush Color="{DynamicResource SelectedBackgroundColor}" /> </Rectangle.Stroke> </Rectangle> <!-- Ajout des couleurs personnalisées pour les fonts --> <Rectangle x:Name="RedDayBackground" IsHitTestVisible="False"> <Rectangle.Fill> <SolidColorBrush Color="{DynamicResource RedDayColor}" /> </Rectangle.Fill> </Rectangle> <Rectangle x:Name="OrangeDayBackground" IsHitTestVisible="False"> <Rectangle.Fill> <SolidColorBrush Color="{DynamicResource OrangeDayColor}" /> </Rectangle.Fill> </Rectangle> </Grid> <ControlTemplate.Triggers> <DataTrigger Binding="{Binding Converter={StaticResource RedDay}}" Value="{x:Null}"> <Setter TargetName="RedDayBackground" Property="Visibility" Value="Hidden"/> </DataTrigger> <DataTrigger Binding="{Binding Converter={StaticResource OrangeDay}}" Value="{x:Null}"> <Setter TargetName="OrangeDayBackground" Property="Visibility" Value="Hidden"/> </DataTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Calendar.CalendarDayButtonStyle> </Calendar> </Grid> </UserControl>
Merci d'avance.
Partager