填充列表框内的网格宽度

填充列表框内的网格宽度

本文介绍了填充列表框内的网格宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格作为列表框中项目的数据模板,它没有填充控件的整个宽度.我已经尝试了其他问题中的建议,但它们不起作用:

I have a grid as the datatemplate for items in a listbox and it's not filling the whole width of the control. I have tried the suggestions in the other questions but they are not working:

这是列表框 xaml

This is the 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>

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

and I am using a theme file from the following project: http://wpfthemes.codeplex.com/ here is the relevant part:

 <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>

我错过了什么吗?

推荐答案

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

You need to make the ListBoxItems stretch their content, either by changing the respective property on the ListBox:

<ListBox HorizontalContentAlignment="Stretch" ...>

...或者通过 ItemContainerStyle 将其设置在项目上:

...or by setting it on the items via the ItemContainerStyle:

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

默认情况下,两者都将作为 ListBoxItem 默认样式将 Horizo​​ntalContentAlignment 属性绑定到拥有的 ListBox 的 属性.

By default both will work as the ListBoxItem default style binds the HorizontalContentAlignment property to the owning ListBox's property.

这篇关于填充列表框内的网格宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:12