Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Stretch Calendar horizontally?

Is there a way to stretch a WPF Calendar control horizontally? The control will let me set HorizontalAlignment="Stretch", but that has the same effect as setting the property to "Center". It changes the width of the control, but the displayed calendar remains the same size in the center of the control. I'd like to stretch the displayed calendar to the edges of the control.

I seem to remember it could be done with a ViewPort, but I can't find anything that shows how to do it. Thanks for your help.

like image 359
David Veeneman Avatar asked Aug 13 '10 01:08

David Veeneman


2 Answers

I think you might be looking for Viewbox:

Defines a content decorator that can stretch and scale a single child to fill the available space.

If you make the Calendar a child of a Viewbox, it will apply a ScaleTransform to make it take up all the available space.

<Viewbox>
    <Calendar/>
</Viewbox>
like image 133
Quartermeister Avatar answered Oct 22 '22 21:10

Quartermeister


I also faced the same issue but i found the answer elsewhere in an incomplete way, so i will leave it here for future reference, after all this was posted 7 years ago!

In my case i wanted to resize the calendar displayed by the DatePicker control.

Create a style for the calendar:

<Style x:Key="styleCalendar" TargetType="{x:Type Calendar}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Calendar}">
                <!-- Wrapping in ViewBox will enlarge calendar of that size.-->
                <Viewbox Height="300">
                    <CalendarItem x:Name="PART_CalendarItem"
                              Background="{TemplateBinding Background}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}"/>
                </Viewbox>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then simply call it in the DatePicker:

<DatePicker CalendarStyle="{StaticResource styleCalendar}"/>

Notice i only specificy the Height in the Viewbox, thats because (from what i read) the ViewBox stretches uniformly, so it will take the smallest of the two dimensions.

like image 41
Luis Ferreira Avatar answered Oct 22 '22 22:10

Luis Ferreira