gpt4 book ai didi

c# - 重新设置 WPF ListView 的 ItemsSource 后,它抛出 ArgumentException

转载 作者:行者123 更新时间:2023-11-30 17:52:10 24 4
gpt4 key购买 nike

我的应用程序中有一个 ListView,目前填充了 2 个项目。

<ListView Name="lstViewFolderSettings" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" SelectionMode="Single" SelectionChanged="lstViewFolderSettings_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Type" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="250" Header="Folder" DisplayMemberBinding="{Binding FolderPath}" />
<GridViewColumn Width="350" Header="XPath" DisplayMemberBinding="{Binding XPath}" />
</GridView>
</ListView.View>
</ListView>

然后我像下面这样设置我的 ItemsSource

lstViewFolderSettings.ItemsSource = fileSeperationSettings.FileSettings;

SelectionChanged 事件中,我得到了填充某些控件的所选项目。然后单击保存,然后更新我的收藏并再次重置 ItemsSource

lstViewFolderSettings.ItemsSource = null;
lstViewFolderSettings.ItemsSource = fileSeperationSettings.FileSettings;

我必须先设置为 null,否则 ListView 不会在 View 中更新

这一切似乎都工作正常,直到我对同一个项目更改我的选择两次。


选择项目 1 -> 更改 -> 更新
选择项目 2
选择项目 1
选择项目 2 -> 砰!

砰!我指的是

ArgumentException 未处理
已添加具有相同键的项目。

堆栈跟踪:

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2..ctor(IDictionary`2 dictionary, IEqualityComparer`1 comparer)
at System.Windows.Controls.Primitives.Selector.InternalSelectedItemsStorage..ctor(InternalSelectedItemsStorage collection, IEqualityComparer`1 equalityComparer)
at System.Windows.Controls.Primitives.Selector.SelectionChanger.ApplyCanSelectMultiple()
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.SetSelectedHelper(Object item, FrameworkElement UI, Boolean selected)
at System.Windows.Controls.Primitives.Selector.NotifyIsSelectedChanged(FrameworkElement container, Boolean selected, RoutedEventArgs e)
at System.Windows.Controls.Primitives.Selector.OnSelected(Object sender, RoutedEventArgs e)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.ListBoxItem.OnSelected(RoutedEventArgs e)

---更新---SelectionChanged 事件处理程序代码。

private void lstViewFolderSettings_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
m_SelectedSetting = lstViewFolderSettings.SelectedItem as FileSetting;
txtFolder.Text = m_SelectedSetting.FolderPath;
txtType.Text = m_SelectedSetting.Name;
txtXPath.Text = m_SelectedSetting.XPath;

e.Handled = true;
}

-- 已更新----

所以我现在有了这个

ObservableCollection<FileSetting> _fileSettings;
public ObservableCollection<FileSetting> FileSettings
{
get
{
if (_fileSettings == null)
{
FileSeperationSettings fileSeperationSettings = m_config.GetSection("fileSeperationSettings") as FileSeperationSettings;

_fileSettings = new ObservableCollection<FileSetting>(fileSeperationSettings.FileSettings.Cast<FileSetting>());
}

return _fileSettings;
}
}

我在这个集合中添加和删除

FileSettings.Add(fsSetting);
FileSettings.Remove(fsSetting);

我得到了选择的项目

m_SelectedSetting = lstViewFolderSettings.SelectedItem as FileSetting;

txtFolder.Text = m_SelectedSetting.FolderPath;
txtType.Text = m_SelectedSetting.Name;
txtXPath.Text = m_SelectedSetting.XPath;

我更新项目

FileSetting fs = FileSettings.First(x => x.Name == m_SelectedSetting.Name);
fs.Name = txtType.Text;
fs.FolderPath = txtFolder.Text;
fs.XPath = txtXPath.Text;

错误发生在我进行更新然后第二次更改所选项目...

最佳答案

我通过阅读以下内容解决了这个问题 can't clear WPF ListBox.SelectedItems collection我意识到它不会删除我的 selectedItem 的原因是因为它不存在于集合中(HashCode 已更改),或者类似的疯狂......

所以我把selectionChanged事件改成了下面的

FileSetting selectedItem;
private void lstViewFolderSettings_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
selectedItem = lstViewFolderSettings.SelectedItem as FileSetting;
txtFolder.Text = selectedItem.FolderPath;
txtType.Text = selectedItem.Name;
txtXPath.Text = selectedItem.XPath;

lstViewFolderSettings.UnselectAll();
}

所以我现在自己跟踪所选项目。这意味着我可以根据需要设置和重新设置 ItemsSource。

关于c# - 重新设置 WPF ListView 的 ItemsSource 后,它抛出 ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18593275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com