我在 Wpf 中有一个扩展器。在页眉中,我将标签左对齐,并希望在右侧站点上有一个按钮。我使用以下 XAML:
<Expander HorizontalAlignment="Stretch" IsExpanded="True">
<Expander.Header >
<Grid HorizontalAlignment="Stretch" Background="Aqua" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Label on the left site"/>
<Button Grid.Column="1" Content="Button on the right"/>
</Grid>
</Expander.Header>
<Label Content="Some Content"/>
</Expander>
但这行不通。标题中的按钮左对齐,在标签旁边。 任何人都可以向我解释如何正确执行此操作吗?
最佳答案
Expander
header content presenter 的水平对齐设置为Left
。
您可以使用 HorizontalAlignment
的 OneWayToSource
绑定(bind)将其更改为 Stretch
(默认情况下 Stretch
Grid
) 像这样:
<Expander>
<Expander.Header>
<Grid Background="Yellow">
<TextBlock Text="Header"
HorizontalAlignment="{Binding HorizontalAlignment, RelativeSource={RelativeSource AncestorType=ContentPresenter}, Mode=OneWayToSource}" />
</Grid>
</Expander.Header>
</Expander>
P.S.:我花了很多时间来理解已接受答案的解决方案,所以我决定为 future 的读者节省时间。
关于c# - WPF Expander.Header 水平拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31161591/