在列表框内填充网格宽度

Mar*_*com 15 wpf layout listbox

我有一个网格作为列表框中项目的数据窗口,它没有填充控件的整个宽度.我在其他问题中尝试过这些建议,但它们没有用到:

这是列表框xaml

 <ListBox ItemsSource="{Binding AccessControl.Credentials}" >                      
                                <ListBox.ItemTemplate>
                            <DataTemplate>
                                    <Grid >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition/>
                                            <RowDefinition/>
                                            <RowDefinition/>
                                        </Grid.RowDefinitions>

                                        <Label Grid.Column="0" Grid.Row="0">Name</Label>
                                        <Label Grid.Column="0" Grid.Row="1">Attribute</Label>
                                        <Label Grid.Column="2" Grid.Row="1">Value</Label>   </Grid>
                            </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
Run Code Online (Sandbox Code Playgroud)

我正在使用以下项目中的主题文件:http://wpfthemes.codeplex.com/这里是相关部分:

 <Style TargetType="{x:Type ListBox}">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="FontFamily" Value="Trebuchet MS" />
    <Setter Property="FontSize" Value="12" />
    <Setter Property="BorderBrush" Value="{DynamicResource ControlBorderBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Padding" Value="1" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Grid>
                    <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" Background="{DynamicResource ControlBackgroundBrush}">
                        <ScrollViewer Margin="1" Focusable="false" Foreground="{TemplateBinding Foreground}">

                            <StackPanel Margin="2" IsItemsHost="true" />

                        </ScrollViewer>
                    </Border>
                    <Border x:Name="DisabledVisualElement" IsHitTestVisible="false" Background="#A5FFFFFF" BorderBrush="#66FFFFFF" BorderThickness="1" Opacity="0" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Opacity" TargetName="DisabledVisualElement" Value="1" />
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

H.B*_*.B. 25

您需要ListBoxItems通过更改以下内容的相应属性来扩展其内容ListBox:

<ListBox HorizontalContentAlignment="Stretch" ...>
Run Code Online (Sandbox Code Playgroud)

...或通过以下方式将其设置在项目上ItemContainerStyle:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
</ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)

默认情况下,两者都可以作为ListBoxItem默认样式将HorizontalContentAlignment属性绑定到拥有ListBox's属性.

  • +1所以我在一个小时结束时发现了这个,搜索如何在列表框中获取网格来填充空间,进入网格共享大小和星形大小的兔子洞,但无济于事。 (2认同)