sch*_*her 6 c# wpf xaml expander
我在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>
Run Code Online (Sandbox Code Playgroud)
但这不起作用.标题中的按钮与标签旁边的左侧对齐.谁能解释我怎么做对了?
Dea*_*unz 16
我能够使用下面提供的xaml在标题中获取内容.基本上我数据绑定网格HorizontailAlignment到内容演示者祖先.不幸的是scher提供的解决方案(数据绑定到ActualWidth)可以使ui元素显示得更宽,然后容器导致控件被部分切断.)Bolu链接到文章" 在Expander Header中拉伸内容 "使用后面的代码作为这个例子使用纯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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
PS:我理解接受答案的解决方案花了我更多的时间,所以我决定为未来的读者节省时间.
我采用了 Bolu 链接的解决方案之一。这是结果:
<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>
Run Code Online (Sandbox Code Playgroud)