在具有不同SelectedBackgroundColor的一个日历控件中选择多个日期

时间:2013-07-23 18:46:20

标签: c# wpf calendar

我想制作一个这样的日历:

http://img826.imageshack.us/img826/7747/60na.png

主要思想是每天选择不同的日子,并在无线电按钮中选择一些颜色。 Radiobuttons一段代码完成并且工作正常,但我不知道如何选择一种颜色的天,因为MultipleRange选择模式,每次都清除选定的日期......

我正在使用风格,但是很复杂...... 这是CalendarD的样式

<Style x:Key="CalendarStyle" TargetType="{x:Type Calendar}">
    <Setter Property="Foreground" Value="{DynamicResource ItemText}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{DynamicResource ItemBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <!--<Setter Property="CalendarButtonStyle" Value="{DynamicResource {x:Type CalendarButton}}"/>-->
    <!--<Setter Property="CalendarDayButtonStyle" Value="{DynamicResource {x:Type CalendarDayButton}}"/>-->
    <Setter Property="CalendarDayButtonStyle" Value="{DynamicResource  CalendarDayButtonStyleOriginal}" />
    <Setter Property="CalendarItemStyle" Value="{DynamicResource  CalendarItemStyle}"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Calendar}">
                <StackPanel x:Name="PART_Root" HorizontalAlignment="Center">
                    <CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Style="{TemplateBinding CalendarItemStyle}"/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="CalendarItemStyle" TargetType="{x:Type CalendarItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CalendarItem}">
                <ControlTemplate.Resources>
                    <DataTemplate x:Key="{ComponentResourceKey ResourceId=DayTitleTemplate, TypeInTargetAssembly={x:Type CalendarItem}}">
                        <TextBlock x:Name="Text" Foreground="{DynamicResource ItemText}" FontWeight="Bold" FontSize="{DynamicResource DefaultFontSize}" HorizontalAlignment="Center" Margin="0,6" Text="{Binding}" VerticalAlignment="Center"/>
                    </DataTemplate>
                </ControlTemplate.Resources>
                <Grid x:Name="PART_Root">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition MinWidth="132" Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>

                            <Button x:Name="PART_HeaderButton" Grid.Column="1" FontWeight="Bold" IsHitTestVisible="False" Focusable="False" HorizontalAlignment="Stretch" Margin="-1,-1,-1,0" Grid.Row="0" VerticalAlignment="Center"/>
                            <Grid x:Name="PART_MonthView" Grid.ColumnSpan="3" HorizontalAlignment="Center" Margin="8" Grid.Row="1" Visibility="Visible">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                            </Grid>

                        </Grid>
                    </Border>
                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是CalendarDayButton

的样式
<Style TargetType="CalendarDayButton"
     x:Key="CalendarDayButtonStyleOriginal">        
    <Setter Property="MinWidth"
      Value="5" />
    <Setter Property="MinHeight"
      Value="5" />
    <Setter Property="FontSize"
      Value="15" />
    <Setter Property="HorizontalContentAlignment"
      Value="Center" />
    <Setter Property="VerticalContentAlignment"
      Value="Center" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CalendarDayButton">
                <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>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

和背景的DynamicResources

<Color x:Key="SelectedBackgroundColor">Crimson</Color>
<Color x:Key="SelectedUnfocusedColor">#FFDDDDDD</Color>

这是代码隐藏:

cal = new Calendar();
cal.Name = "MonthlyCalendar";
cal.DisplayMode = CalendarMode.Month;
cal.SelectionMode = CalendarSelectionMode.MultipleRange;
cal.Style = (Style)App.Current.Resources["CalendarStyle"];

任何帮助??

0 个答案:

没有答案