嵌套ListBox与DataTemplates的边距差异

时间:2014-03-12 10:16:49

标签: c# wpf listbox margin datatemplate

我的ListBox有问题。我已经编写了几个我在ListBox中使用的DataTemplates。其中每个都包含一个网格,可能包含一个嵌套的ListBox,具体取决于项目。 我的问题是:这些嵌套ListBoxes的高度似乎与根ListBox的高度不同。此外,似乎上面的元素有时会有一个像素边距。

NestedGrids

是否有人遇到过这个问题并且可能已经解决了?

XAML的代码:

         <!-- Template for SubData-Items -->
         <DataTemplate x:Key="DataTemplate">

            <Grid x:Name="baseGrid" Grid.Column="0" Grid.ColumnSpan="999" Background="Violet">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="{Binding ElementName=Col0, Path=Width}" />
                    <ColumnDefinition Width="{Binding ElementName=Col1, Path=Width}" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="{Binding ElementName=Row0, Path=Height}"/>
                </Grid.RowDefinitions>

                <TextBlock Grid.Column="1" Text="{Binding Bezeichnung}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="12" />
                <Line Grid.ColumnSpan="999" Stroke="Black" X1="0" X2="{Binding ElementName=baseGrid, Path=ActualWidth, Mode=OneWay}" Y1="0" Y2="0" VerticalAlignment="Bottom" />

            </Grid>

        </DataTemplate>

        <!-- Template for Items -->
        <DataTemplate x:Key="GroupDataTemplate">

            <Grid Grid.ColumnSpan="999" Background="Blue">

                <Grid.RowDefinitions>
                    <RowDefinition Height="{Binding ElementName=Gantt, Path=GridRowHeight}" />
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Grid x:Name="baseGrid" Grid.Column="0" Grid.ColumnSpan="999">

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="{Binding ElementName=Col0, Path=Width}" />
                        <ColumnDefinition Width="{Binding ElementName=Col1, Path=Width}" />
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="{Binding ElementName=Row0, Path=Height}"/>
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Column="1" Text="{Binding Bezeichnung}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="12" />

                </Grid>

                <Grid x:Name="expandedGrid" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="999">

                    <ListBox x:Name="LBMaGruppen" ItemTemplate="{StaticResource DataTemplate}" ItemsSource="{Binding SubdataObjects}"                                                                                
                                    VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Height="Auto" Margin="0,0,2,0"
                                    ScrollViewer.CanContentScroll="false" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" BorderThickness="0" />

                </Grid>

            </Grid>

        </DataTemplate>

   <!-- Grid with ListBox -->
   <Grid>

        <Grid.RowDefinitions>
            <RowDefinition x:Name="Row0" Height="34"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="Col0" Width="25" />
            <ColumnDefinition x:Name="Col1" Width="*" />
        </Grid.ColumnDefinitions>

        <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="999" ItemsSource="{Binding ItemSource, Mode=OneWay}"
            ItemTemplate="{StaticResource GroupDataTemplate}"
            VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="Auto"               
            ScrollViewer.CanContentScroll="true"
            ScrollViewer.IsDeferredScrollingEnabled="False"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled"
            ScrollViewer.VerticalScrollBarVisibility="Disabled"
            FontSize="10" />

    </Grid>

3 个答案:

答案 0 :(得分:2)

qqbenq提供了一个非常好的解决方案。这是另一种方法。

ListBox和ListBoxItem的控件模板在其边框内部有填充。值得庆幸的是,ListBoxItem边框的填充可以通过如下样式控制:

    <Style
        TargetType="ListBoxItem">
        <Setter
            Property="Padding"
            Value="0" />
    </Style>

但遗憾的是,对于ListBox,您必须覆盖默认模板及其所有细节,以更改其内部边框的硬编码填充:

    <Style
        TargetType="ListBox">
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type ListBox}">
                    <Border
                        x:Name="Bd"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        Padding="0"
                        SnapsToDevicePixels="true">
                        <ScrollViewer
                            Focusable="false"
                            Padding="{TemplateBinding Padding}">
                            <ItemsPresenter
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger
                            Property="IsEnabled"
                            Value="false">
                            <Setter
                                Property="Background"
                                TargetName="Bd"
                                Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                        </Trigger>
                        <Trigger
                            Property="IsGrouping"
                            Value="true">
                            <Setter
                                Property="ScrollViewer.CanContentScroll"
                                Value="false" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

最后一点:删除你在xaml中添加的保证金。

解决方案完成

答案 1 :(得分:1)

将Margin =“ - 2,0,0,0”和Padding =“ - 1”添加到名为ListBox的“LBMaGruppen”中。

<ListBox x:Name="LBMaGruppen" ItemTemplate="{StaticResource DataTemplate}" ItemsSource="{Binding SubdataObjects}"                                                                                
                                VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Height="Auto" Margin="-2,0,0,0" Padding="-1"
                                ScrollViewer.CanContentScroll="false" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" BorderThickness="0" />

Running with those modifications

此外,“root”元素的高度绑定到{Binding ElementName = Gantt,Path = GridRowHeight},而“sub”元素的高度为{Binding ElementName = Row0,Path = Height},因此这必须导致差异

答案 2 :(得分:0)

您的所有ListBox似乎都设置了自动高度。您可以尝试将高度设置为特定大小。编辑:我看到你已经有ScrollViewer设置。我认为将盒子的高度设置为特定的大小而不是自动将做你想要的。

关于元素之间的填充,请参阅ListBox.ItemContainerSyle,它应该允许您设置元素之间的边距。