【问题标题】:Avoiding spaces while grouping with listbox使用列表框进行分组时避免使用空格
【发布时间】:2011-08-24 13:08:09
【问题描述】:

我正在使用列表框分组,我想避免在标题和子元素之间留下垂直空格,如下图:

这是相同的 xaml

<Grid>
    <Grid.Resources>
        <XmlDataProvider x:Key="Company" XPath="/Company">
            <x:XData>
                <Company xmlns="">
                    <Person Name="Jack" Role="CEO"/>
                    <Person Name="Tim" Role="PL" />
                    <Person Name="Jil" Role="PL" />
                    <Person Name="Jimmy" Role="PM" />
                    <Person Name="Joy" Role="PM" />
                    <Person Name="Jim" Role="PL" />
                    <Person Name="Jack" Role="PM" />
                </Company>
            </x:XData>
        </XmlDataProvider>

        <DataTemplate x:Key="categoryTemplate">
            <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Background="ForestGreen" Margin="0,5,0,0"/>
        </DataTemplate>
        <DataTemplate x:Key="template">
            <TextBlock Text="{Binding XPath=@Name}"/>
        </DataTemplate>

        <CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource Company},XPath=Person}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@Role"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Grid.Resources>


    <ListBox Name="lst" ItemTemplate="{StaticResource template}" ItemsSource="{Binding Source={StaticResource cvs}}">
        <ListBox.GroupStyle>
            <GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />
        </ListBox.GroupStyle>
    </ListBox>
</Grid>

【问题讨论】:

  • 你试过用宽度缩小网格尺寸吗?

标签: wpf xaml listbox wpf-controls


【解决方案1】:

ListBox 将在您分组时使用 GroupItems,并且它们的默认 Margin 设置为 5,0,0,0。此外,ListBoxItem 带有默认的 Padding2,0,0,0。您可以像这样更改其中一个或两个

<ListBox ...>
    <ListBox.Resources>
        <Style TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <StackPanel>
                            <ContentPresenter/>
                            <ItemsPresenter Margin="0,0,0,0"/>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <!--...-->
</ListBox>

【讨论】:

    【解决方案2】:

    检查这是否适合您。刚刚修改了你的模板位。

    <DataTemplate x:Key="template">
        <TextBlock Text="{Binding XPath=@Name}" Margin="-5,0,0,0"/>
    </DataTemplate> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多