WPF在Calendar中添加新的依赖项属性

时间:2015-05-11 19:12:54

标签: c# wpf calendar dependency-properties

我需要帮助创建一个新的依赖属性,功能与SelectedDates相同。

逻辑有效:使用SelectedDates选择日,按顶部按钮"设置"要从SelectedDates添加新属性值,请设置另一种颜色并将值写入文本文档。

此外,应该可以从文本文档中读取这些日期。当您运行程序时,设置日期05.13.2015,05.14.2015,05.15.2015应该已经突出显示。

我需要创建一个新的依赖项属性,您可以在其中选择不同的颜色来突出显示日期并编写文本文件。由于功能与SelectedDates相同。

(抱歉我的英语不好)

http://s57.radikal.ru/i155/1505/a4/7d10f56aa52e.png

风格:

    <ResourceDictionary
        xmlns:local="clr-namespace:CustomCalendar"
        xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
        xmlns:primitives="clr-  namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"
        xmlns:vsm="clr-namespace:System.Windows;assembly=WPFToolkit">

    <!--CalendarItem-->
    <Style x:Key="CalendarItemStyle" TargetType="primitives:CalendarItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="primitives:CalendarItem">
                    <ControlTemplate.Resources>

                        <!-- Start: Data template for header button -->
                        <DataTemplate x:Key="DayTitleTemplate">
                            <Grid>
                                <TextBlock FontWeight="Normal" FontFamily="Arial" FontSize="13.4" Foreground="#FF063B83" HorizontalAlignment="Center" Text="{Binding}" Margin="0,3,0,3" VerticalAlignment="Center"/>
                            </Grid>
                        </DataTemplate>
                        <!-- End: Data template for header button -->        
                    </ControlTemplate.Resources>

                    <Grid Name="PART_Root">
                        <Grid.Resources>
                            <SolidColorBrush x:Key="DisabledColor" Color="#A5FFFFFF" />
                        </Grid.Resources>
                        <vsm:VisualStateManager.VisualStateGroups>
                            <vsm:VisualStateGroup x:Name="CommonStates">
                                <vsm:VisualState x:Name="Normal" />
                                <vsm:VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="PART_DisabledVisual" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                    </Storyboard>
                                </vsm:VisualState>
                            </vsm:VisualStateGroup>
                        </vsm:VisualStateManager.VisualStateGroups>

                        <Border BorderBrush="#d3dadb" BorderThickness="3" Background="{TemplateBinding Background}" CornerRadius="3">
                            <Border>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>

                                    <Grid.Resources>
                                        <!-- Start: Previous button template -->
                                        <ControlTemplate x:Key="PreviousButtonTemplate" TargetType="Button">
                                            <Grid Cursor="Hand">
                                                <vsm:VisualStateManager.VisualStateGroups>
                                                    <vsm:VisualStateGroup x:Name="CommonStates">
                                                        <vsm:VisualState x:Name="Normal" />
                                                        <vsm:VisualState x:Name="MouseOver">
                                                            <Storyboard>
                                                                <ColorAnimation Storyboard.TargetName="TextColor" Storyboard.TargetProperty="Color" To="#FF73A9D8" Duration="0" />
                                                            </Storyboard>
                                                        </vsm:VisualState>
                                                        <vsm:VisualState x:Name="Disabled">
                                                            <Storyboard>
                                                                <DoubleAnimation Storyboard.TargetName="TextColor" Storyboard.TargetProperty="Opacity" To=".5" Duration="0" />
                                                            </Storyboard>
                                                        </vsm:VisualState>
                                                    </vsm:VisualStateGroup>
                                                </vsm:VisualStateManager.VisualStateGroups>
                                                <Rectangle Fill="#11E5EBF1" Stretch="Fill" Opacity="1"/>
                                                <Grid>
                                                    <Path Margin="4,0,0,0" Height="10" Width="6" VerticalAlignment="Center" HorizontalAlignment="Left" Stretch="Fill" Data="M288.75,232.25 L283,236.625 L288.75,240.625" StrokeThickness="2">
                                                        <Path.Stroke>
                                                            <SolidColorBrush x:Name="TextColor" Color="#FF063B83" />
                                                        </Path.Stroke>
                                                    </Path>
                                                    <Path x:Name="TextColor2" Margin="4,0,0,0" Height="10" Width="12" VerticalAlignment="Center" HorizontalAlignment="Left" Stretch="Fill" Data="M283,236.625 L297,236.625" Stroke="#FF063B83" StrokeThickness="2"/>
                                                </Grid>
                                            </Grid>
                                        </ControlTemplate>
                                        <!-- End: Previous button template -->

                                        <!-- Start: Next button template -->
                                        <ControlTemplate x:Key="NextButtonTemplate" TargetType="Button">
                                            <Grid Cursor="Hand">
                                                <vsm:VisualStateManager.VisualStateGroups>
                                                    <vsm:VisualStateGroup x:Name="CommonStates">
                                                        <vsm:VisualState x:Name="Normal" />
                                                        <vsm:VisualState x:Name="MouseOver">
                                                            <Storyboard>
                                                                <ColorAnimation Storyboard.TargetName="TextColor" Storyboard.TargetProperty="Color" To="#FF73A9D8" Duration="0" />
                                                            </Storyboard>
                                                        </vsm:VisualState>
                                                        <vsm:VisualState x:Name="Disabled">
                                                            <Storyboard>
                                                                <DoubleAnimation Storyboard.TargetName="TextColor" Storyboard.TargetProperty="Opacity" To=".5" Duration="0" />
                                                            </Storyboard>
                                                        </vsm:VisualState>
                                                    </vsm:VisualStateGroup>
                                                </vsm:VisualStateManager.VisualStateGroups>
                                                <Rectangle Fill="#11E5EBF1" Stretch="Fill" Opacity="1"/>
                                                <Grid>
                                                    <Path Margin="0,0,4,0" Height="10" Width="6" VerticalAlignment="Center" HorizontalAlignment="Right" Stretch="Fill" Data="M283,232.25 L288.75,236.625 L283,240.625" StrokeThickness="2">
                                                        <Path.Stroke>
                                                            <SolidColorBrush x:Name="TextColor" Color="#FF063B83" />
                                                        </Path.Stroke>
                                                    </Path>
                                                    <Path Margin="4,0,0,0" Height="10" Width="12" VerticalAlignment="Center" HorizontalAlignment="Left" Stretch="Fill" Data="M283,236.625 L297,236.625" Stroke="#FF063B83" StrokeThickness="2"/>
                                                </Grid>
                                            </Grid>
                                        </ControlTemplate>
                                        <!-- End: Next button template -->

                                        <!-- Start: Header button template -->
                                        <ControlTemplate x:Key="HeaderButtonTemplate" TargetType="Button">
                                            <Grid Cursor="Hand">
                                                <vsm:VisualStateManager.VisualStateGroups>
                                                    <vsm:VisualStateGroup x:Name="CommonStates">
                                                        <vsm:VisualState x:Name="Normal" />
                                                        <vsm:VisualState x:Name="MouseOver">
                                                            <Storyboard>
                                                                <ColorAnimation Storyboard.TargetName="TextColor" Storyboard.TargetProperty="Color" To="#FF73A9D8" Duration="0" />
                                                            </Storyboard>
                                                        </vsm:VisualState>
                                                        <vsm:VisualState x:Name="Disabled">
                                                            <Storyboard>
                                                                <DoubleAnimation Storyboard.TargetName="buttonContent" Storyboard.TargetProperty="Opacity" To=".5" Duration="0" />
                                                            </Storyboard>
                                                        </vsm:VisualState>
                                                    </vsm:VisualStateGroup>
                                                </vsm:VisualStateManager.VisualStateGroups>
                                                <ContentPresenter x:Name="buttonContent" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                                    <TextElement.Foreground>
                                                        <SolidColorBrush x:Name="TextColor" Color="#FF063B83"/>
                                                    </TextElement.Foreground>
                                                </ContentPresenter>
                                            </Grid>
                                        </ControlTemplate>
                                        <!-- End: Header button template -->
                                    </Grid.Resources>

                                    <Rectangle Fill="#FFEDEDED" Grid.ColumnSpan="4" Height="35"/>

                                    <!-- Start: Previous button content -->
                                    <Button x:Name="PART_PreviousButton" Margin="4" Grid.Row="0" Grid.Column="0" Template="{StaticResource PreviousButtonTemplate}" Height="20" Width="20" HorizontalAlignment="Left" Focusable="False"/>
                                    <!-- End: Previous button content -->

                                    <!-- Start: Header button content -->
                                    <Button x:Name="PART_HeaderButton" Grid.Row="0" Grid.ColumnSpan="3" Template="{StaticResource HeaderButtonTemplate}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17" Focusable="False" FontWeight="SemiBold" FontFamily="Arial"  Foreground="#FF063B83"/>
                                    <!-- End: Header button content -->

                                    <!-- Start: Next button content -->
                                    <Button x:Name="PART_NextButton" Margin="4" Grid.Row="0" Grid.Column="2" Height="20" Width="20" HorizontalAlignment="Right" Template="{StaticResource NextButtonTemplate}" Focusable="False"/>
                                    <!-- End: Next button content -->

                                    <!-- Start: Month Content Grid -->
                                    <Grid x:Name="PART_MonthView" Grid.Row="1" Grid.ColumnSpan="4" Visibility="Visible">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                    </Grid>
                                    <!-- End: Month Content Grid -->

                                    <!-- End: Year Content Grid -->
                                    <Grid x:Name="PART_YearView" Grid.Row="1" Grid.ColumnSpan="3" Visibility="Hidden">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                    </Grid>
                                    <!-- End: Year Content Grid -->
                                </Grid>
                            </Border>
                        </Border>
                        <Rectangle x:Name="PART_DisabledVisual" Opacity="0" Visibility="Collapsed" Stretch="Fill" StrokeThickness="1" RadiusX="2" RadiusY="2" Stroke="{StaticResource DisabledColor}" Fill="{StaticResource DisabledColor}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="PART_DisabledVisual" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <DataTrigger Value="Year">
                            <DataTrigger.Binding>
                                <Binding Path="DisplayMode">
                                    <Binding.RelativeSource>
                                        <RelativeSource Mode="FindAncestor" AncestorType="{x:Type toolkit:Calendar}" />
                                    </Binding.RelativeSource>
                                </Binding>
                            </DataTrigger.Binding>
                            <Setter TargetName="PART_MonthView" Property="Visibility" Value="Hidden" />
                            <Setter TargetName="PART_YearView" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <DataTrigger Value="Decade">
                            <DataTrigger.Binding>
                                <Binding Path="DisplayMode">
                                    <Binding.RelativeSource>
                                        <RelativeSource Mode="FindAncestor" AncestorType="{x:Type toolkit:Calendar}" />
                                    </Binding.RelativeSource>
                                </Binding>
                            </DataTrigger.Binding>
                            <Setter TargetName="PART_MonthView" Property="Visibility" Value="Hidden" />
                            <Setter TargetName="PART_YearView" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--CalendarDayButton-->
    <Style x:Key="CalendarDayButtonStyle" TargetType="primitives:CalendarDayButton">
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="MinWidth" Value="5"/>
        <Setter Property="MinHeight" Value="5"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="primitives:CalendarDayButton">
                    <ControlTemplate.Resources>
                        <LinearGradientBrush x:Key="gradient" EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFF9FAFB" Offset="0"/>
                            <GradientStop Color="#FFE9E9E9" Offset="1"/>
                        </LinearGradientBrush>
                    </ControlTemplate.Resources>
                    <Grid>

                        <Rectangle x:Name="Background" Grid.Row="1" RadiusX="1" RadiusY="1" Fill="{StaticResource gradient}" />

                        <Border>
                            <ContentPresenter x:Name="NormalText" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="Center">
                            </ContentPresenter>
                        </Border>

                        <Rectangle x:Name="Border" StrokeThickness="0.2" Grid.RowSpan="2" SnapsToDevicePixels="True">
                            <Rectangle.Stroke>
                                <SolidColorBrush x:Name="BorderBrush" Color="#FF5D8CC9"/>
                            </Rectangle.Stroke>
                        </Rectangle>

                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsToday" Value="True">
                            <Setter TargetName="Background" Property="Stroke" Value="#FF063B83"/>
                            <Setter Property="Foreground" Value="#FF063B83"/>
                        </Trigger>

                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="#FF5D8CC9"/>
                        </Trigger>

                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="Background" Property="Fill" Value="#bec9d7"/>
                        </Trigger>                     

                        <Trigger Property="IsInactive" Value="True">
                            <Setter TargetName="Background" Property="Fill" Value="White"/>
                            <Setter Property="Foreground" Value="#bdbdbd"/>
                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="Background" Property="Fill" Value="Black"/>
                            <Setter Property="Foreground" Value="#bdbdbd"/>
                        </Trigger>

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




    <!--Calendar-->
    <Style TargetType="local:MyCalendar">
        <Setter Property="CalendarItemStyle" Value="{StaticResource CalendarItemStyle}"/>
        <Setter Property="CalendarDayButtonStyle" Value="{StaticResource CalendarDayButtonStyle}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="toolkit:Calendar">
                    <Grid Name="PART_Root">
                        <primitives:CalendarItem Name="PART_CalendarItem" Style="{TemplateBinding CalendarItemStyle}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

MyCalendar.cs

namespace CustomCalendar
{

    public class MyCalendar : Calendar
    {
        static MyCalendar()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCalendar), new FrameworkPropertyMetadata(typeof(MyCalendar)));
        }

    }
}

0 个答案:

没有答案