WPF C# 自定义控件——如何让子级遵循严格的尺寸(伸展)
我不知道如何解决这个问题,但我创建了一个自定义面板 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的自定义面板中,您只能给孩子们一些空间。之后,由子元素决定如何在该空间中对齐自己。此行为由
FrameworkElement
的HorizontalAlignment
/VerticalAlignment
属性控制。如果这两个属性都设置为Stretch
,那么该元素将拉伸,占据您在面板内分配给它们的所有空间。更新:
除了
HorizontalContentAllignment
/VerticalContentAlignment
之外,将HorizontalAlignment
/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 aFrameworkElement
. If both of those properties are set toStretch
then the element will stretch taking all the space you gave to them inside your panel.Update:
In addition to
HorizontalContentAllignment
/VerticalContentAlignment
set theHorizontalAlignment
/VerticalAlignment
properties toStretch
in the style ofFooChild
element.您是否尝试设置HorizontalAlignment =“Stretch”或HorizontalContentAlignment =“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. :)
您可以将所有子项填充到
DockPanel
控件中,确保它们是唯一的子项,然后将LastChildFill
属性设置为True
。或者,您可以覆盖 MeasureOverride 属性您所有的孩子,并迫使他们全部扩展到可用的最大空间。
You could stuff all of your children into
DockPanel
controls, make sure they are the only child and then set theLastChildFill
property toTrue
.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.
就在几周前,我遇到了这样的问题。
我这样修复了它,但我注意到这不是首选方法。
这是针对
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.
This is for a
ListBox
but I imagine it works for any ItemsControl.Source: another SO question