开发者

WPF: Stretch Calendar horizontally?

开发者 https://www.devze.com 2023-01-11 14:58 出处:网络
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 chang

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. 开发者_JAVA技巧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.


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消