绑定到 DataView 时如何删除 ListViewItem

发布于 2024-08-08 23:49:23 字数 336 浏览 2 评论 0原文

当 ItemsSource 设置为 DataView 时,如何从 WPF ListView 中删除选定的 ListViewItem?我可以获得所选的 ListViewItem,然后如何删除 DataView 中的实际行?

DataView dv = (DataView)myListView.ItemsSource;
ListViewItem lvi = (ListViewItem)myListView.ItemContainerGenerator.ContainerFromItem(myListView.SelectedItem);
<Delete ListViewItem here> 

How do I delete a selected ListViewItem from a WPF ListView when the ItemsSource is set to a DataView? I can get the ListViewItem that was selected and then how do remove the actual row in the DataView?

DataView dv = (DataView)myListView.ItemsSource;
ListViewItem lvi = (ListViewItem)myListView.ItemContainerGenerator.ContainerFromItem(myListView.SelectedItem);
<Delete ListViewItem here> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-08-15 23:49:23

将集合绑定到列表视图时,请使用 ListCollectionView 而不是 DataView。可以像这样轻松完成(其中 dataView 是 DataView 类型):

ListCollectionView lcv = new ListCollectionView(dataView);
myListView.ItemsSource = lcv;

现在,当您需要删除任何对象时,只需执行以下操作:

ListCollectionView lcv = (ListCollectionView) myListView.ItemsSource;
lcv.Remove(myListView.SelectedItem);

删除后,只需刷新视图:

lcv.Refresh();

((ListCollectionView)myListView.ItemsSource).Refresh();

When you bind your collection to the listview, use ListCollectionView instead of DataView. Can be easily done like this (where dataView is of type DataView):

ListCollectionView lcv = new ListCollectionView(dataView);
myListView.ItemsSource = lcv;

Now when you need to delete any object, just do this:

ListCollectionView lcv = (ListCollectionView) myListView.ItemsSource;
lcv.Remove(myListView.SelectedItem);

And after deleting, just refresh the view:

lcv.Refresh();

or

((ListCollectionView)myListView.ItemsSource).Refresh();
枕花眠 2024-08-15 23:49:23

考虑使用 MV-VM 模式来分离从数据对象列表中删除项目的概念,并直接从当前 UI 实现中删除它们。除了绑定之外,两者不需要互相了解。

当您使用 MVVM 模式时,请在 ViewModel 中公开布尔值“IsSelected”属性。

public class SimpleViewModel : BaseViewModel //For INotifyPropertyChanged, etc
{

      public IList<SimpleBusinessObject> ViewModelItems;

      public SimpleViewModel()
      {
             ViewModelItems = new ObservableList<SimpleBusinessObjectViewModel>();
      }

}

public class SimpleBusinessObjectViewModel
{
      public bool ViewModelIsSelected { get; set; }

      public SimpleBusinessObjectViewModel()
      {
             ViewModelIsSelected = false;
      }
}

接下来,在您的视图中尝试如下操作:

<Style TargetType="{x:Type ListViewItem}">
       <Style.Triggers>
               <Setter Property="IsSelected" Value="{Binding ViewModelIsSelected}"
       </Style.Triggers>
</Style>
<ListView ItemsSource={Binding ViewModelItems}>   
       //here you can insert how you want to display a ListViewItem
</ListView>

这将允许您添加、编辑和删除 ViewModel 列表中的项目 - 就像它是实际的 ListView 一样。从这里,您还可以检查每个项目的 IsSelected(响应鼠标与 ListView 的交互),而无需实际检查 ListViewItem。这将是一个更干净、可维护的解决方案。

Consider using the M-V-VM pattern to separate the notion of removing an item from your list of data objects and DIRECTLY removing them from your current UI implementation. The two do not need to know about each other, aside from Bindings.

When you use the MVVM pattern, expose a boolean "IsSelected" property in your ViewModel.

public class SimpleViewModel : BaseViewModel //For INotifyPropertyChanged, etc
{

      public IList<SimpleBusinessObject> ViewModelItems;

      public SimpleViewModel()
      {
             ViewModelItems = new ObservableList<SimpleBusinessObjectViewModel>();
      }

}

public class SimpleBusinessObjectViewModel
{
      public bool ViewModelIsSelected { get; set; }

      public SimpleBusinessObjectViewModel()
      {
             ViewModelIsSelected = false;
      }
}

Next, in your View try something like this:

<Style TargetType="{x:Type ListViewItem}">
       <Style.Triggers>
               <Setter Property="IsSelected" Value="{Binding ViewModelIsSelected}"
       </Style.Triggers>
</Style>
<ListView ItemsSource={Binding ViewModelItems}>   
       //here you can insert how you want to display a ListViewItem
</ListView>

This will let you add, edit, and remove items in your ViewModel's List -- just like if it were the actual ListView. From here, you can also check each item's IsSelected (that responds to mouse interactions with the ListView) without actually checking the ListViewItem. This will be a much cleaner, maintainable solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文