locked
WPF Calendar: making invisible arrows at header RRS feed

  • Question

  • Hi there,

    As you will see in the following picture I would like to make invisible left & right arrows. Is it possible?

    TIA,

    Sunday, March 16, 2014 8:39 PM

Answers

  • The following link has a list of the WPF controls at the bottom of the page.  Within each link there is a section called "ControlName Parts".  If there are items within the template that the method I showed you would work, those parts would be found there.  A quick look at the list shows that most controls don't have named parts but if you look at the Calendar control in the list you will find the parts list.  I think this control has these since as most people have found out there is no easy method to change the look and feel of the calendar.

    Each control has its template shown so if you need  to change a style you would have a place to start.

    http://msdn.microsoft.com/en-us/library/aa970773(v=vs.110).aspx


    Lloyd Sheen

    • Marked as answer by Enric Vives Monday, March 17, 2014 12:05 AM
    Sunday, March 16, 2014 11:37 PM

All replies

  • The following code will allow you to find the arrows in the control.

        Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
            Dim myButton As Button = FindVisualChildNamed(Of Button)(theCalendar, "PART_PreviousButton")
            Dim mybutton2 As Button = FindVisualChildNamed(Of Button)(theCalendar, "PART_NextButton")
            myButton.Visibility = Windows.Visibility.Hidden
            mybutton2.Visibility = Windows.Visibility.Hidden
        End Sub
    
        Private Function FindVisualChildNamed(Of childItem As DependencyObject)(ByVal obj As DependencyObject, theName As String) As childItem
            For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
                Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)
                If child IsNot Nothing AndAlso TypeOf child Is childItem AndAlso CType(child, Button).Name = theName Then
                    Dim ii As Integer = 1
                    Return CType(child, childItem)
                Else
                    Dim childOfChild As childItem = FindVisualChildNamed(Of childItem)(child, theName)
                    If childOfChild IsNot Nothing Then
                        Dim ii As Integer = 1
                        Return childOfChild
                    End If
                End If
            Next i
            Return Nothing
        End Function
    


    Lloyd Sheen

    Sunday, March 16, 2014 10:19 PM
  • Lloyd,  very useful.

    I had found another solution just one hour ago but your solutions it's better.

    It was to write one Style calling primitives:CalendarItem



    XAML view:

    <Calendar Name="TercerMes" Canvas.Left="194" Canvas.Top="10"

                             CalendarItemStyle="{StaticResource Calendariet}"

                         

                          />

    Styles.XAML:


    <Style x:Key="Calendariet"
               TargetType="primitives:CalendarItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="primitives:CalendarItem">
                    <ControlTemplate.Resources>
                        <DataTemplate x:Key="DayTitleTemplate">
                            <TextBlock Text="{Binding}"
                                           HorizontalAlignment="Center" />
                        </DataTemplate>
                    </ControlTemplate.Resources>
    
                    <DockPanel Name="PART_Root" 
                                   LastChildFill="True">
                        <Button x:Name="PART_PreviousButton" 
                                    DockPanel.Dock="Left"
                                    Content="&lt;"
                                    Focusable="False" 
                                    Visibility="Hidden"
                                    />
    
                        <Button x:Name="PART_NextButton" 
                                    DockPanel.Dock="Right"
                                    Content="&gt;" 
                                    Focusable="False" 
                                    Visibility="Hidden"
                                    />
    
                        <Button x:Name="PART_HeaderButton"                                             
                                    DockPanel.Dock="Top"
                                    FontWeight="Bold"
                                    Focusable="False" />
    
                        <Grid>
                            <Grid x:Name="PART_MonthView" 
                                      Visibility="Visible">
                                <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.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>
    
                            <Grid x:Name="PART_YearView" 
                                      Visibility="Hidden">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                            </Grid>
                        </Grid>
                        <Rectangle x:Name="PART_DisabledVisual" Opacity="0" Visibility="Collapsed" Fill="#A5FFFFFF"/>
                    </DockPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="PART_DisabledVisual" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Calendar}}, Path=DisplayMode}" Value="Year">
                            <Setter TargetName="PART_MonthView" Property="Visibility" Value="Hidden" />
                            <Setter TargetName="PART_YearView" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Calendar}}, Path=DisplayMode}" Value="Decade">
                            <Setter TargetName="PART_MonthView" Property="Visibility" Value="Hidden" />
                            <Setter TargetName="PART_YearView" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



    • Edited by Enric Vives Sunday, March 16, 2014 10:56 PM
    Sunday, March 16, 2014 10:55 PM
  • Lloyd,

    Just a question.

    How did you know that in that control "PART_PreviousButton" and "PART_NextButton" were the names that you had to look for??

    I mean, how did you know that they played the role for the arrows??

    Generally speaking, how can I figure out this kind of things?

    Imagine that I've got another control and need to do something similar


    • Edited by Enric Vives Sunday, March 16, 2014 11:11 PM
    Sunday, March 16, 2014 11:11 PM
  • The following link has a list of the WPF controls at the bottom of the page.  Within each link there is a section called "ControlName Parts".  If there are items within the template that the method I showed you would work, those parts would be found there.  A quick look at the list shows that most controls don't have named parts but if you look at the Calendar control in the list you will find the parts list.  I think this control has these since as most people have found out there is no easy method to change the look and feel of the calendar.

    Each control has its template shown so if you need  to change a style you would have a place to start.

    http://msdn.microsoft.com/en-us/library/aa970773(v=vs.110).aspx


    Lloyd Sheen

    • Marked as answer by Enric Vives Monday, March 17, 2014 12:05 AM
    Sunday, March 16, 2014 11:37 PM