Home > Enterprise >  How to remove the square in the bottom right corner of a listview in wpf?
How to remove the square in the bottom right corner of a listview in wpf?

Time:09-02

I'm trying to remove that square from the list view, but I searched everywhere how to remove it and i tried to code it, but I don't know much of WPF.

This is an image of what it looks like.

https://i.imgur.com/95Ej6lC.png

It needs to be completely transparent because I'm using acrylic effect.

I need help on this, I've been 4 days searching for a solution and I ended up creating a new account on stack overflow because I didn't find anything helpfull.

I would really appreciate if someone help me.

CodePudding user response:

enter image description here

You need to find the Rectangle(that you want to change color) using the VisualTreeHelper.

Sample MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Height="600" Width="800">
    <Grid>
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFB98E8E"/>
                <GradientStop Color="#FF4C789A" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
    
        <ListView x:Name="listView" Width="150" Height="150" Background="Transparent" Loaded="listView_Loaded"/>
    </Grid>
</Window>

Sample MainWindow.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        listView.ItemsSource = new List<string>
        {
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
            "Item1Item1Item1Item1Item1Item1Item1",
        };
    }

    private void listView_Loaded(object sender, RoutedEventArgs e)
    {
        ListView listView = (ListView)sender;
        Border border = (Border)VisualTreeHelper.GetChild(listView, 0);
        ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
        Grid grid = (Grid)VisualTreeHelper.GetChild(scrollViewer, 0);
        Rectangle rectangle = (Rectangle)VisualTreeHelper.GetChild(grid, 0);

        // change the color!!
        rectangle.Fill = new SolidColorBrush(Colors.Transparent);
    }
}

If you are using a different template or style, my code is not Working

I can't give you exact help because I don't know the listview template you currently have.

If it is difficult to find the Rectangle.. try this code,

<ListView x:Name="listView" Width="150" Height="150" Background="Transparent"
          Loaded="listView_Loaded"
          PreviewMouseDown="listView_PreviewMouseDown"/>

Click on the element you want to delete,

    private void listView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        // inputElement is the element you clicked on
        IInputElement inputElement = System.Windows.Input.Mouse.DirectlyOver;
    }

Explore the visual tree with its elements in debug mode

  • Related