简体   繁体   中英

Grid and Listbox have some Border i cannot get rid of

I'm quite new to WPF. I just try some layouting with Grid and Listbox, but I have some padding / spacing / margin / border (just call it border for shortness) that I can not get away.

The border is around the four elements, the elements themselves have no problems and there is no space between them.

Also tried the WPF Inspector, but I can't find out where this comes from. Nothing to see in it.

Here is my 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>

Hope someone can help me get rid of this border. Thank you so much!

The problem is the first Border inside the ListBox Template , it has Padding set to 1,1,1,1 .
It looks like this

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

As you can see in the Template , the Border name is Bd.

Either change the Template for the ListBox or set it to 0 in the Loaded event

Xaml

<ListBox ...
         Loaded="ListBox_Loaded">

Code behind

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);
}

Have you tried setting ControlTemplate for the ListBox or your Grid. You should be able to get rid of borders by writing your own template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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