Removing From ListView ItemTemplate

BitSmithy 1,256 Reputation points


I have a ListView defined as:

Now I want to Remove from ItemTemplate Button. I want to do this code behind.

Tried to do this with code:
ElementFactoryGetArgs efga = new ElementFactoryGetArgs();
Grid g = (Grid)lvProjects.ItemTemplate.GetElement(efga);

But I failed. Please answer how to remove button from the ItemTemplate code behind.

Universal Windows Platform (UWP)
{count} votes

3 answers

Sort by: Most helpful
  1. BitSmithy 1,256 Reputation points


        <ListView x:Name="lvProjects" x:Uid="lvProjects" Margin="0,10,20,0" Foreground="Blue"
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="31"></ColumnDefinition>
                        <TextBox x:Name="tbxProjectName" Text="{Binding showedName, UpdateSourceTrigger=Explicit, Mode=TwoWay}"                                                                                                                  
                                 Grid.Column="0" HorizontalAlignment ="Stretch" BorderThickness="0"                                  >
                        <Button x:Name="btnProjectItemButton" Content=".." Grid.Column="1" HorizontalAlignment="Right" Margin="5,0,0,0"                                                                                        

  2. Roy Li - MSFT 28,236 Reputation points Microsoft Vendor


    Welcome to Microsoft Q&A!

    Based on your scenario, you don't need to remove the button from the template. My suggestion is you could just change the visibility of the button to achieve the same behavior- make the button disappear. This will also be more easy to control in case you want to bring the button back.

    To reach this goal, you will need to implement the INotifyPropertyChanged interface and add a new bool property in the data model. Like the following code:

    public class Test :INotifyPropertyChanged  
            private string _showedName { get; set; }  
            private bool _buttonVisibility { get; set; }  
            public string showedName  
                get { return _showedName; }  
                    _showedName = value;  
            public bool buttonVisibility   
                get { return _buttonVisibility; }  
                    _buttonVisibility = value;  
            public event PropertyChangedEventHandler PropertyChanged;  
            protected void RaisePropertyChanged(string name)  
                if (PropertyChanged != null)  
                    PropertyChanged(this, new PropertyChangedEventArgs(name));  

    And in the Xaml, you need to bind the visibility property.

    TextBox x:Name="tbxProjectName" Text="{Binding showedName, UpdateSourceTrigger=Explicit,Mode=TwoWay}" Grid.Column="0" HorizontalAlignment ="Stretch" BorderThickness="0"  
    Button x:Name="btnProjectItemButton" Content=".." Grid.Column="1" HorizontalAlignment="Right" Margin="5,0,0,0" Visibility="{Binding buttonVisibility,Mode=TwoWay}"  

    After that, when you change the bool value of the data model, the button will disappear.

  3. Martin-AF 1 Reputation point

    Get the ListviewItem first using Listview.ContainerFromItem(Object). Then you can get the button by going through all the child elements using VisualTreeHelper. Finally you could remove the button as you want.
    See the code below

    //ListviewItem is the row where you want to remove the button. Get the object using  Listview.ContainerFromItem(Object)
       int count = VisualTreeHelper.GetChildrenCount( ListViewItem );
        for( int i = 0; i < count; i++ )
            UIElement child = ( UIElement )VisualTreeHelper.GetChild( parent, i );
            if( FindControl( child, ControlName ) != null )
                result = FindControl( child, ControlName );