Sabir
Sabir

Reputation: 233

Remove Item in Listview

I am created a listview and add combobox and button control into listview and a button to remove items in this view.I need to remove Listview item when clicking the Remove button inside the listview without clicking the row.

 <ListView Name="listView">            
        <ListView.View>
            <GridView>
                <GridViewColumn Header="SlNo" Width="40">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtSlno" Width="35" Text="{Binding slno}" />                                       
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Bar Code" Width="120">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="cmbBarcode" IsEditable="True" Width="110" Text="{Binding barcode}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Item Name" Width="250">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="txtitemname" IsEditable="True" Width="240" Text="{Binding itemname}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Qty" Width="65">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtqty" Width="65" Text="{Binding qty}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Unit" Width="65">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtunit" Width="60" Text="{Binding unit}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Rate" Width="90">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtrate" Width="85" Text="{Binding rate}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Disc %" Width="80">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtdiscp" Width="75" Text="{Binding discp}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Disc" Width="80">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtdisc" Width="75" Text="{Binding disc}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Net Amount" Width="130">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtnetamount" Width="125" Text="{Binding netamount}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="MRP" Width="130">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="txtmrp" Width="125" Text="{Binding mrp}" PreviewKeyDown="txtmrp_PreviewKeyDown"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Remove" Width="70">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button x:Name="btnRemove" Content="Remove" Width="60" BorderThickness="0" CommandParameter="{Binding}" HorizontalContentAlignment="Right"  Cursor="Hand" Foreground="Blue" Click="btnEdit_Click"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <Border CornerRadius="2" SnapsToDevicePixels="True"
                            BorderThickness="{TemplateBinding     BorderThickness}" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            Background="{TemplateBinding Background}">
                                <Border Name="InnerBorder" CornerRadius="1"   BorderThickness="1">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition MaxHeight="11" />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <Rectangle Name="UpperHighlight" Visibility="Collapsed" Fill="#75FFFFFF" />
                                        <GridViewRowPresenter Grid.RowSpan="2" 
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                    </Grid>
                                </Border>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Background" Value="LightBlue"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

The c# code is given below

private void btnEdit_Click(object sender, RoutedEventArgs e)
{
   listView.Items.RemoveAt(listView.SelectedIndex);
}

It working properly when first click on listview item and then click the remove button.I need to move item with one click on Remove button

Upvotes: 1

Views: 5824

Answers (3)

Abhinav Pandey
Abhinav Pandey

Reputation: 87

// Try This Code

 String txt = ((TextView)view).getText().toString();
 adapter.remove(txt);

enter image description here

Upvotes: 0

user3345626
user3345626

Reputation: 243

I used an ICommand instead of button click events since I wanted to put the Command in the parent viewmodel instead of the code-behind, but I'll share anyways since it works for me and could be helpful for you.

Instead of Click="btnEdit_Click", I have Command="{Binding ParentViewModel.btnEditCommand". (I also have CommandParameter="{Binding}" as you do.)

Then in the ParentViewModel, called "MyViewModel":

public ObservableCollection<ListItemsViewModel> ListItems
    {
        get;
        set;
    }

public MyViewModel()
{
    btnEditCommand = new RelayCommand<object>(btnEditCommand_Do, btnEditCommand_Can);
}

public ICommand btnEditCommand
{
    get; 
    set;
}

public bool btnEditCommand_Can( object param )
{
    return true;
}

public void btnEditCommand_Do( object param )
{
    ListItems.Remove( param as ListItemsViewModel );
}

and finally, the ListItemsViewModel referenced above inherits from the same parent as MyViewModel inherits from.

Hope this helps.

Upvotes: 1

paparazzo
paparazzo

Reputation: 45096

If you are using binding this is how you get the DataContext to remove the row from the source

Button btn = (Button)sender;
SearchItem srchItem = (SearchItem)btn.DataContext;

Upvotes: 1

Related Questions