Daniel Breul
Daniel Breul

Reputation: 13

How to remove border from ListBox in WPF?

Here is my Code:

<Grid>
    <ScrollViewer Grid.Row="0" Grid.Column="1" HorizontalScrollBarVisibility="Disabled">
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                <TextBox MinWidth="80" Name="tbTodoName" Margin="5, 2"/>
                <Button Content="Add" Height="30" Margin="5, 0"/>
            </StackPanel>
            <ListBox Name="lstTodo" ItemsSource="{Binding}" BorderThickness="0" Padding="0" ItemTemplate="{StaticResource TodoTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch">
            </ListBox>
        </StackPanel>
    </ScrollViewer>
</Grid>

and here is a picture of what the program looks like:

and here is a picture of what the program looks like

As can be seen in the picture, a frame is displayed around the ListBox. I don't understand why, since I set BorderThickness and Padding = "0".

Can someone help me?

Upvotes: 0

Views: 421

Answers (1)

AmmarArnt
AmmarArnt

Reputation: 114

You have set a custom ItemTemplate, which is applied only to the items.

You will need to apply the Template as well:

    <Grid>
        <ScrollViewer Grid.Row="0" Grid.Column="1" HorizontalScrollBarVisibility="Disabled">
            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                    <TextBox MinWidth="80" Name="tbTodoName" Margin="5, 2"/>
                    <Button Content="Add" Height="30" Margin="5, 0"/>
                </StackPanel>
                <ListBox Name="lstTodo" ItemsSource="{Binding}" BorderThickness="0" Padding="0" ItemTemplate="{StaticResource TodoTemplate}" Template={StaticResource ListBoxNoBorder} 
ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch">
                </ListBox>
            </StackPanel>
        </ScrollViewer>
    </Grid>

And in your resource dictionary:

<ControlTemplate x:Key="ListBoxNoBorder" TargetType="{x:Type ListBox}">
    <Border BorderBrush="Transparent" BorderThickness="0">
        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
    </Border>
</ControlTemplate>

Upvotes: 1

Related Questions