我创建了一个listview并将组合框和按钮控件添加到listview中,并添加了一个按钮来删除此视图中的项目。我需要在单击listview中的Remove按钮时删除Listview项而不单击该行。
<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>
c#代码如下所示
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
listView.Items.RemoveAt(listView.SelectedIndex);
}
首次点击listview项目然后点击删除按钮时,它正常工作。我只需点击一下删除按钮即可移动项目
答案 0 :(得分:0)
如果您正在使用绑定,那么就是如何让DataContext从源
中删除行Button btn = (Button)sender;
SearchItem srchItem = (SearchItem)btn.DataContext;
答案 1 :(得分:0)
我使用ICommand而不是按钮点击事件,因为我想将Command放在父视图模型而不是代码隐藏中,但我会分享,因为它对我有用,可能对你有所帮助。
而不是Click="btnEdit_Click"
,我有Command="{Binding ParentViewModel.btnEditCommand"
。 (我也像你一样CommandParameter="{Binding}"
。)
然后在ParentViewModel中,名为“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 );
}
最后,上面引用的ListItemsViewModel继承自MyViewModel继承自的相同父级。
希望这有帮助。