繁体   English   中英

网格和列表框有一些我无法摆脱的边框

[英]Grid and Listbox have some Border i cannot get rid of

我对WPF很新。 我只是尝试使用Grid和Listbox进行一些布局,但是我有一些填充/间距/边距/边框(简称为边框),我无法逃脱。

边界围绕四个元素,元素本身没有问题,它们之间没有空间。

还尝试了WPF Inspector,但我无法找到它的来源。 没有什么可看的。

这是我的XAML:

<Window x:Class="WpfElements.FourElements"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FourElements" Height="701" Width="351">
<Grid Background="Red" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <ListBox Margin="0" BorderThickness="0" Padding="0" Grid.Row="0" Grid.Column="0" Background="Lime" SelectionMode="Single" ItemsSource="{Binding Path=Texts}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding ItemText}" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Col}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0"></TextBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Column" Value="{Binding Path=Col}"/>
                <Setter Property="Grid.Row" Value="{Binding Path=Row}"/>
                <Setter Property="ContentControl.HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="ContentControl.VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="ContentControl.Margin" Value="0"/>
                <Setter Property="ContentControl.Padding" Value="0"/>
                <Setter Property="Control.BorderThickness" Value="0"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

希望有人可以帮助我摆脱这个边界。 非常感谢!

问题是ListBox Template的第一个Border ,它的Padding设置为1,1,1,1
看起来像这样

<Style TargetType="{x:Type ListBox}" ...>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border x:Name="Bd"
                        Padding="1">
                <!-- ... -->

正如您在Template看到的, Border名称是Bd。

更改ListBoxTemplate或在Loaded事件中将其设置为0

XAML

<ListBox ...
         Loaded="ListBox_Loaded">

代码背后

private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
    ListBox listBox = sender as ListBox;
    Border Bd = listBox.Template.FindName("Bd", listBox) as Border;
    Bd.Padding = new Thickness(0);
}

您是否尝试过为ListBox或Grid设置ControlTemplate。 您应该能够通过编写自己的模板来摆脱边界。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM