我在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>
但这不起作用。标题中的按钮与标签旁边的左侧对齐。 谁能解释我怎么做对了?
答案 0 :(得分:14)
我能够使用下面提供的xaml在标题中获取内容。基本上我数据绑定网格HorizontailAlignment到内容演示者祖先。不幸的是scher提供的解决方案(数据绑定到ActualWidth)可以使ui元素显示得更宽,然后容器导致控件被部分切断。)Bolu链接到文章&#34; Stretching Content in an Expander Header &#34;使用代码后面的代码,因为此示例使用纯xaml。
<ItemsControl x:Name="ItemGroups" Grid.Column="2" Grid.Row="0" ItemsSource="{Binding Model.ItemGroups}" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Margin="4,0" Header="{Binding}">
<Expander.HeaderTemplate>
<DataTemplate>
<Grid HorizontalAlignment="{Binding Path=HorizontalAlignment, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}" >
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="64"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding Name, Mode=TwoWay}" />
<TextBlock Grid.Column="1" Text="{Binding TotalCostString}" Margin="4,0"/>
<Button Grid.Column="2" Command="{Binding DataContext.RemoveItemGroup, ElementName=ItemGroups, Mode=OneWay}" CommandParameter="{Binding}" Content="Remove"/>
</Grid>
</DataTemplate>
</Expander.HeaderTemplate>
<Expander.Content>
<TextBlock Text="{Binding Summary}"></TextBlock>
</Expander.Content>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
答案 1 :(得分:6)
Expander
标题内容展示者的水平对齐设置为Left
。
您可以使用Stretch
绑定OneWayToSource
(HorizontalAlignment
默认为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。:我理解接受答案的解决方案比我理解的要多,所以我决定为未来的读者节省时间。
答案 2 :(得分:3)
我采用了博卢联系的解决方案之一。这是结果:
<Expander HorizontalAlignment="Stretch" IsExpanded="True">
<Expander.Header >
<!-- Width-Binding is needed, to fill the whole header horizontally-->
<Grid HorizontalAlignment="Stretch" Background="Aqua" Margin="0" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Label on the left site"/>
<!-- Margin is needed, to bring the Button into the view -->
<Button Grid.Column="1" Content="Button on the right" Margin="0,0,40,0"/>
</Grid>
</Expander.Header>
<Label Content="Some Content"/>
</Expander>
答案 3 :(得分:0)
*哎呀:正确填充到例如100000:Padding="2 2 100000 2"
<Expander Name="Name" ExpandDirection="Down">
<Expander.Header>
<TextBlock Text="Expander..." Margin="0 5" Padding="2 2 100000 2" />
</Expander.Header>
</Expander>
答案 4 :(得分:0)
如果您不想更改Expander Header,那么也可以使用在加载Header时修改HorizontalAlignment的Behaviors(System.Windows.Interactivity)
internal class StretchingExpanderHeaderBehavior : Behavior<Expander>
{
protected override void OnAttached()
{
AssociatedObject.Loaded += StretchHeader;
}
private void StretchHeader(object sender, RoutedEventArgs e)
{
DependencyObject header = AssociatedObject.Header as DependencyObject;
if (header != null)
{
ContentPresenter contentPresenter = VisualTreeHelper.GetParent(header) as ContentPresenter;
if (contentPresenter != null)
{
contentPresenter.HorizontalAlignment = HorizontalAlignment.Stretch;
}
}
}
}
这可以附加到扩展器上
<Expander>
<Expander.Header>
<DockPanel HorizontalAlignment="Stretch">
<Button DockPanel.Dock="Right" Content="Button on the right" />
<Label Content="Label on the left site"/>
</DockPanel>
</Expander.Header>
<Border BorderBrush="LightGray" BorderThickness="1">
<ItemsPresenter />
</Border>
<iy:Interaction.Behaviors>
<bhv:StretchingExpanderHeaderBehavior />
</iy:Interaction.Behaviors>
</Expander>