Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带数据模板的嵌套列表框中的边距差异_C#_Wpf_Listbox_Margin_Datatemplate - Fatal编程技术网

C# 带数据模板的嵌套列表框中的边距差异

C# 带数据模板的嵌套列表框中的边距差异,c#,wpf,listbox,margin,datatemplate,C#,Wpf,Listbox,Margin,Datatemplate,我的列表框有问题。我已经编写了几个在列表框中使用的数据模板。其中每一个都包含一个网格,并且可能包含一个嵌套的列表框,具体取决于项目。 我的问题是:这些嵌套列表框的高度似乎与根列表框的高度不同。此外,似乎上面的元素有时有一个像素的边距 有没有人也遇到过这个问题,也许已经解决了 XAML代码: <!-- Template for SubData-Items --> <DataTemplate x:Key="DataTemplate">

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

有没有人也遇到过这个问题,也许已经解决了

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>

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

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

在名为“LBMaGruppen”的列表框中添加边距=“-2,0,0”和填充=“-1”

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


此外,“根”元素的高度绑定到{Binding ElementName=Gantt,Path=GridRowHeight},而“子”元素的高度绑定到{Binding ElementName=Row0,Path=height},因此这一定会导致差异。

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中添加的边距


解决方案完成

您好,请使用XAML更新您的问题,以便我们了解您是如何嵌套所有内容的。在您的子项列表框数据模板中,您将行高绑定到“带列表框的网格”中的行高,我确信这不会起作用,但更重要的是,我不确定您是否希望这样做!通过将嵌套列表框设置为与父列表框相同的大小,只会导致父列表框增长。你到底想达到什么目的?如果它正在改变边距,你可以尝试使用负值来删除你看到的额外的一个像素。我试图实现的是列表中的每一行都具有相同的高度,因为它必须适合其右侧的视觉元素,该元素具有垂直绘制的线。我还尝试给它们一个负的边距,但这只会导致网格使上面的行不可见。如果您有嵌套的datatemplates,您应该使用
Hierarchy DataTemplate
,而不是常规的
DataTemplate
。另外,为什么要通过绑定设置行和列的宽度和高度?为什么不使用SharedSizeGroup,您是否尝试过使用它?将高度设置为特定值不是解决方案,因为每个项都可以有x个子项。我以前不知道。scrollviewer必须只显示在根列表框中,因为其中的行必须适合其右侧的可视元素。玩保证金属性也不起作用。+1和赏金提供了一个简单而干净的解决方案。没有考虑设置padding属性。谢谢