C# WPF ListView 没有正确删除项目
C# WPF ListView not deleting items properly
我有一个 ListView
看起来像这样;
<Window x:Class="WPF_Viewer.URLLinks"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Viewer"
mc:Ignorable="d"
Title="URLLinks" Height="461.215" Width="606.542" WindowStartupLocation="CenterScreen">
<Grid>
<ListView x:Name="proxyListView" Loaded="proxyListView_Loaded">
<ListView.ContextMenu>
<ContextMenu Name="contextMenu1">
<MenuItem Name="item1" Header="Delete" Click="item1_Click"/>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
</Grid>
</Window>
我正在尝试添加一个 ContextMenu
来删除 SelectedItem
这是后面的代码;
public partial class URLLinks : Window
{
public URLLinks()
{
InitializeComponent();
}
private void proxyListView_Loaded(object sender, RoutedEventArgs e)
{
proxyListView.ItemsSource = GlobalVars.URLLinks;
}
private void item1_Click(object sender, RoutedEventArgs e)
{
GlobalVars.URLLinks.RemoveAt(proxyListView.SelectedIndex);
ICollectionView view = CollectionViewSource.GetDefaultView(proxyListView.ItemsSource);
//view.Refresh();
}
}
有什么想法吗?
编辑:这是变量及其设置方式
public static async void GetLink(string url)
{
try
{
string sURL = url;
Uri uri = new Uri(sURL);
string host = uri.Host;
using (HttpClient clientduplicate = new HttpClient())
{
clientduplicate.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)");
using (HttpResponseMessage responseduplicate = await clientduplicate.GetAsync(sURL))
{
using (HttpContent contentduplicate = responseduplicate.Content)
{
try
{
string resultduplicate = await contentduplicate.ReadAsStringAsync();
var websiteduplicate = new HtmlAgilityPack.HtmlDocument();
websiteduplicate.LoadHtml(resultduplicate);
List<string> ListItems = new List<string>();
Settings.Default.Reload();
int maxLinks = Convert.ToInt32(Math.Round(Convert.ToDouble(Settings.Default["subLinksValue"]) * 10));
foreach (HtmlNode links in websiteduplicate.DocumentNode.SelectNodes("//a[@href]"))
{
HtmlAttribute att = links.Attributes["href"];
foreach (var link in att.Value.Split(' '))
{
if (ListItems.Count >= maxLinks)
{
GlobalVars.URLLinks = ListItems;
//File.WriteAllLines(AppDomain.CurrentDomain.BaseDirectory + @"\links.txt", ListItems.ToArray());
return;
}
if (link.StartsWith("http") && link.Contains(host) && CheckURLValid(link))
{
ListItems.Add(link);
}
}
}
GlobalVars.URLLinks = ListItems;
//File.WriteAllLines(AppDomain.CurrentDomain.BaseDirectory + @"\links.txt", ListItems.ToArray());
//return ListItems;
}
catch (Exception ex1)
{
Console.WriteLine("Error getting links");
//return null;
}
}
}
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("Error, url not formatted correctly. Try coping your url from your browser");
}
}
即使您不打算走完整的 MVVM 路线,至少使用项目的 ObservableCollection 并将您的 ListView 的 ItemSource 分配给它。这将在删除或添加项目时自动更新控件。
正在尝试对 U.I 执行业务逻辑操作。控制很快就会退化成一团意大利面。
如果 GlobalVars.URLLinks
returns 一个 ObservableCollection<T>
或实现 INotifyCollectionChanged
接口的任何其他自定义类型,这将起作用:
private void item1_Click(object sender, RoutedEventArgs e)
{
var ic = proxyListView.ItemsSource as IList<string>;
ic.RemoveAt(proxyListView.SelectedIndex);
}
但是如果源集合在项目被删除时没有引发更改通知(List<T>
没有),您将不得不重新设置 ItemsSource
属性删除项目后刷新 ListView
:
private void item1_Click(object sender, RoutedEventArgs e)
{
GlobalVars.URLLinks.RemoveAt(proxyListView.SelectedIndex);
proxyListView.ItemsSource = GlobalVars.URLLinks;
}
我唯一要做的就是将你的 collection 从 List<string>
更改为 ObservableCollection<string>
。
完成后,您无需重置列表视图中的项目。
static ObservableCollection<string> items = new ObservableCollection<string>() { "Item 1", "Item 2" };
private void proxyListView_Loaded(object sender, RoutedEventArgs e)
{
proxyListView.ItemsSource = items;
}
private void item1_Click(object sender, RoutedEventArgs e)
{
items.RemoveAt(proxyListView.SelectedIndex);
}
我有一个 ListView
看起来像这样;
<Window x:Class="WPF_Viewer.URLLinks"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Viewer"
mc:Ignorable="d"
Title="URLLinks" Height="461.215" Width="606.542" WindowStartupLocation="CenterScreen">
<Grid>
<ListView x:Name="proxyListView" Loaded="proxyListView_Loaded">
<ListView.ContextMenu>
<ContextMenu Name="contextMenu1">
<MenuItem Name="item1" Header="Delete" Click="item1_Click"/>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
</Grid>
</Window>
我正在尝试添加一个 ContextMenu
来删除 SelectedItem
这是后面的代码;
public partial class URLLinks : Window
{
public URLLinks()
{
InitializeComponent();
}
private void proxyListView_Loaded(object sender, RoutedEventArgs e)
{
proxyListView.ItemsSource = GlobalVars.URLLinks;
}
private void item1_Click(object sender, RoutedEventArgs e)
{
GlobalVars.URLLinks.RemoveAt(proxyListView.SelectedIndex);
ICollectionView view = CollectionViewSource.GetDefaultView(proxyListView.ItemsSource);
//view.Refresh();
}
}
有什么想法吗?
编辑:这是变量及其设置方式
public static async void GetLink(string url)
{
try
{
string sURL = url;
Uri uri = new Uri(sURL);
string host = uri.Host;
using (HttpClient clientduplicate = new HttpClient())
{
clientduplicate.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)");
using (HttpResponseMessage responseduplicate = await clientduplicate.GetAsync(sURL))
{
using (HttpContent contentduplicate = responseduplicate.Content)
{
try
{
string resultduplicate = await contentduplicate.ReadAsStringAsync();
var websiteduplicate = new HtmlAgilityPack.HtmlDocument();
websiteduplicate.LoadHtml(resultduplicate);
List<string> ListItems = new List<string>();
Settings.Default.Reload();
int maxLinks = Convert.ToInt32(Math.Round(Convert.ToDouble(Settings.Default["subLinksValue"]) * 10));
foreach (HtmlNode links in websiteduplicate.DocumentNode.SelectNodes("//a[@href]"))
{
HtmlAttribute att = links.Attributes["href"];
foreach (var link in att.Value.Split(' '))
{
if (ListItems.Count >= maxLinks)
{
GlobalVars.URLLinks = ListItems;
//File.WriteAllLines(AppDomain.CurrentDomain.BaseDirectory + @"\links.txt", ListItems.ToArray());
return;
}
if (link.StartsWith("http") && link.Contains(host) && CheckURLValid(link))
{
ListItems.Add(link);
}
}
}
GlobalVars.URLLinks = ListItems;
//File.WriteAllLines(AppDomain.CurrentDomain.BaseDirectory + @"\links.txt", ListItems.ToArray());
//return ListItems;
}
catch (Exception ex1)
{
Console.WriteLine("Error getting links");
//return null;
}
}
}
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("Error, url not formatted correctly. Try coping your url from your browser");
}
}
即使您不打算走完整的 MVVM 路线,至少使用项目的 ObservableCollection 并将您的 ListView 的 ItemSource 分配给它。这将在删除或添加项目时自动更新控件。
正在尝试对 U.I 执行业务逻辑操作。控制很快就会退化成一团意大利面。
如果 GlobalVars.URLLinks
returns 一个 ObservableCollection<T>
或实现 INotifyCollectionChanged
接口的任何其他自定义类型,这将起作用:
private void item1_Click(object sender, RoutedEventArgs e)
{
var ic = proxyListView.ItemsSource as IList<string>;
ic.RemoveAt(proxyListView.SelectedIndex);
}
但是如果源集合在项目被删除时没有引发更改通知(List<T>
没有),您将不得不重新设置 ItemsSource
属性删除项目后刷新 ListView
:
private void item1_Click(object sender, RoutedEventArgs e)
{
GlobalVars.URLLinks.RemoveAt(proxyListView.SelectedIndex);
proxyListView.ItemsSource = GlobalVars.URLLinks;
}
我唯一要做的就是将你的 collection 从 List<string>
更改为 ObservableCollection<string>
。
完成后,您无需重置列表视图中的项目。
static ObservableCollection<string> items = new ObservableCollection<string>() { "Item 1", "Item 2" };
private void proxyListView_Loaded(object sender, RoutedEventArgs e)
{
proxyListView.ItemsSource = items;
}
private void item1_Click(object sender, RoutedEventArgs e)
{
items.RemoveAt(proxyListView.SelectedIndex);
}