我的应用程序中有一个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
在视图中不会更新。这一切似乎都很好,直到我在同一项上两次更改我的选择。
即:选择项目1->更改->更新,选择项目2,选择项目1,选择项目2->BANG!
我所指的BANG!是 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;
在我进行更新并第二次更改所选项目后,出现了错误...
ObserableCollection<T>
或类似类型,其中每个项都实现了INotifyPropertyChanged
。一旦所有这些都设置好了(使用空列表),并绑定到listview,您就可以添加项目,编辑它们,删除它们等等,数据绑定将自动更新UI。 - Neil Barnwell