简体   繁体   English

如何在网格中为扩展器提供全宽列

[英]How to give Expander Full Width of Column in a Grid

I have an Expander and it should receive the full width of the column but I cant get it to work.我有一个Expander ,它应该接收列的整个宽度,但我无法让它工作。

I tried to add the Horizontal(Content)Alignment = stretch on the TextBlock and on the Expander itself even on the Grid , but it is not working.我尝试在TextBlockExpander本身甚至在Grid上添加Horizontal(Content)Alignment = stretch ,但它不起作用。

What I need is that the Expander takes about 90% of the width and the rest are assigned to the buttons as in the following example:我需要的是Expander占据大约 90% 的宽度,并且 rest 被分配给按钮,如下例所示:

扩展器项目在 ListView 中占据全宽的示例。

I want to display eg a name and when you press on it, expands down and shows additional information and if the buttons are pressed, then the commands behind the buttons will be executed (no commands are binded in the example).我想显示一个名字,当你按下它时,它会向下展开并显示附加信息,如果按下按钮,那么按钮后面的命令将被执行(示例中没有绑定命令)。

<ListView
    ItemsSource="{Binding log}"
    SelectionMode="Multiple"
    Style="{StaticResource ListViewStyle}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0,0,0,1" BorderBrush="Gray">
                <Grid HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100*" />
                        <ColumnDefinition Width="5*" />
                        <ColumnDefinition Width="5*" />
                    </Grid.ColumnDefinitions>

                    <Expander Grid.Column="0"
                              HorizontalAlignment="Stretch"
                              IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}}"
                              Style="{StaticResource ExpanderStyle}">
                        <Expander.Header>
                            <TextBlock Text="{Binding Name}" Foreground="White"
                                       HorizontalAlignment="Stretch" />
                        </Expander.Header>
                        <TextBlock
                            VerticalAlignment="Center"
                            FontSize="16"
                            Foreground="White"
                            Text="{Binding Description}" />
                    </Expander>

                    <Button Grid.Column="1"
                            Style="{StaticResource IconButton}">
                        <Button.Background>
                            <ImageBrush ImageSource="../icons/edit.png"
                                        Stretch="None" />
                        </Button.Background>
                    </Button>
                    <dialogButton:ConfirmButton Grid.Column="2"
                                                Question="{x:Static language:Strings.confirm}"
                                                Style="{DynamicResource IconButton}"
                                                Margin="0,0,10,0">
                        <dialogButton:ConfirmButton.Background>
                            <ImageBrush ImageSource="../icons/delete.png"
                                        Stretch="None" />
                        </dialogButton:ConfirmButton.Background>
                    </dialogButton:ConfirmButton>

                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Styles Styles

<Style x:Key="ExpanderDownHeaderStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border Padding="{TemplateBinding Padding}">
                    <Grid Background="Transparent" SnapsToDevicePixels="False">
                        <ContentPresenter HorizontalAlignment="Stretch" Margin="0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Expander}">
                <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" SnapsToDevicePixels="true">
                    <DockPanel>
                        <ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" DockPanel.Dock="Top" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontWeight="{TemplateBinding FontWeight}" FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual}" FontFamily="{TemplateBinding FontFamily}" Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" MinHeight="0" MinWidth="0" Margin="1" Padding="{TemplateBinding Padding}" Style="{StaticResource ExpanderDownHeaderStyle}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        <ContentPresenter x:Name="ExpandSite" DockPanel.Dock="Bottom" Focusable="false" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Visibility="Collapsed"/>
                    </DockPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded" Value="true">
                        <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
                    </Trigger>

                    <Trigger Property="ExpandDirection" Value="Down">
                        <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Bottom"/>
                        <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Top"/>
                        <Setter Property="Background" Value="Transparent" />
                        <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderDownHeaderStyle}"/>
                    </Trigger>

                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I also tried things from the following Threads我还尝试了以下线程中的内容

wpf - Header of Expander fit contents width? 扩展器适合内容宽度的 wpf - Header?

Problem here is that the Button s are pushed out of the view这里的问题是Button被推出视图

Best Solution would be if the Expander just takes the width of the column so that I can adjust the widths of the element by the Grid.ColumnDefinition .最好的解决方案是Expander只采用列的宽度,以便我可以通过Grid.ColumnDefinition调整元素的宽度。

The issue is that the item container , a ListViewItem does not stretch its content by default.问题是item containerListViewItem默认情况下不会拉伸其内容。 You have to create an item container style in order to set the HorizontalContentAlignment to Stretch .您必须创建一个项目容器样式才能将HorizontalContentAlignment ntalContentAlignment 设置为Stretch

<ListView.ItemContainerStyle>
   <Style TargetType="{x:Type ListViewItem}">
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
   </Style>
</ListView.ItemContainerStyle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM