WPF C# 自定义控件——如何让子级遵循严格的尺寸(伸展)

发布于 2024-10-30 05:41:02 字数 1712 浏览 1 评论 0原文

我不知道如何解决这个问题,但我创建了一个自定义面板 FooPanel。我希望我的 FooPanel 强制所有子项适应面板预先确定的尺寸(这样它们是统一的)。我了解 UniformGrid 但我还有一些其他内容。

我不知道如何强迫孩子们适应我告诉他们的盒子。


Child.xaml:

<Style TargetType="{x:Type l:FooChild}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Padding" Value="12,2,12,2" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Background" Value="White" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:FooChild}">
                <Border x:Name="ChromeBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <ContentPresenter ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                                      VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                                      />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I'm not sure how to go about this, but I created a custom Panel FooPanel. I want my FooPanel to force all the children to fit within a size that is predetermined by the panel (that way they are uniform). I know about UniformGrid but I also have some other stuff going into it.

I'm not sure how to force the children to fit within the box I tell them to.


Child.xaml:

<Style TargetType="{x:Type l:FooChild}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Padding" Value="12,2,12,2" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Background" Value="White" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:FooChild}">
                <Border x:Name="ChromeBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <ContentPresenter ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                                      VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                                      />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

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

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

发布评论

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

评论(4

别在捏我脸啦 2024-11-06 05:41:02

在您的自定义面板中,您只能给孩子们一些空间。之后,由子元素决定如何在该空间中对齐自己。此行为由 FrameworkElementHorizo​​ntalAlignment/VerticalAlignment 属性控制。如果这两个属性都设置为 Stretch,那么该元素将拉伸,占据您在面板内分配给它们的所有空间。

更新

除了 Horizo​​ntalContentAllignment/VerticalContentAlignment 之外,将 Horizo​​ntalAlignment/VerticalAlignment 属性设置为以 FooChild 元素的样式进行 Stretch

In your custom panel you can only give the children some space. After that, it is up to child elements how to align themselves in that space. This behavior is controlled by the HorizontalAlignment/VerticalAlignment properties of a FrameworkElement. If both of those properties are set to Stretch then the element will stretch taking all the space you gave to them inside your panel.

Update:

In addition to HorizontalContentAllignment/VerticalContentAlignment set the HorizontalAlignment/VerticalAlignment properties to Stretch in the style of FooChild element.

浊酒尽余欢 2024-11-06 05:41:02

您是否尝试设置Horizo​​ntalAlignment =“Stretch”或Horizo​​ntalContentAlignment =“Stretch”(并且有一个VertialAlignment)

您可以提供一些xaml吗?这样我也许能更好地帮助你。 :)

Did you try setting HorizontalAlignment="Stretch" or HorizontalContentAlignment="Stretch" (and there's a VertialAlignment)

Could you provide some xaml? I may be able to help you better that way. :)

情丝乱 2024-11-06 05:41:02

您可以将所有子项填充到 DockPanel 控件中,确保它们是唯一的子项,然后将 LastChildFill 属性设置为 True

或者,您可以覆盖 MeasureOverride 属性您所有的孩子,并迫使他们全部扩展到可用的最大空间。

You could stuff all of your children into DockPanel controls, make sure they are the only child and then set the LastChildFillproperty to True.

Alternatively, you could override the MeasureOverride property on all of your children and force them all to expand to the maximum amount of space available to them.

路还长,别太狂 2024-11-06 05:41:02

就在几周前,我遇到了这样的问题。
我这样修复了它,但我注意到这不是首选方法。

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>

这是针对 ListBox 的,但我想它适用于任何 ItemsControl。

来源:另一个问题

I had a problem like this only a couple of weeks ago.
I fixed it like this, but I noticed it's not the preferred method.

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>

This is for a ListBox but I imagine it works for any ItemsControl.

Source: another SO question

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