WPF:水平拉伸日历?

发布于 2024-09-13 19:23:26 字数 196 浏览 5 评论 0原文

有没有办法水平拉伸 WPF 日历控件?该控件允许我设置 Horizo​​ntalAlignment="Stretch",但这与将属性设置为“Center”具有相同的效果。它更改控件的宽度,但显示的日历在控件中心保持相同的大小。我想将显示的日历拉伸到控件的边缘。

我似乎记得它可以用 ViewPort 来完成,但我找不到任何显示如何做到这一点的内容。感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2024-09-20 19:23:26

我想您可能正在寻找 Viewbox

定义一个内容装饰器,可以
拉伸和缩放单个孩子
填充可用空间。

如果您将日历设为 Viewbox 的子级,它将应用 ScaleTransform 以使其占据所有可用空间。

<Viewbox>
    <Calendar/>
</Viewbox>

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>
迷荒 2024-09-20 19:23:26

我也遇到了同样的问题,但我在其他地方以不完整的方式找到了答案,所以我将其留在这里以供将来参考,毕竟这是7年前发布的!

就我而言,我想调整 DatePicker 控件显示的日历的大小。

为日历创建一个样式:

<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>

然后只需在 DatePicker 中调用它:

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

注意,我只指定 Viewbox 中的高度,那是因为(根据我读到的) 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文