2014-11-14 74 views
2

我使用Caliburn.micro做一个正则表达式库中的图形用户界面,并且在大多数情况下它的工作原理。 但是,当您尝试加载新字典或正则表达式时,将抛出未处理的异常并且程序崩溃。WPF + Caliburn.Micro:组合框和列表框不与字典更新正确

在我的壳观点XAML,这里是绑定到枚举的键值对的字典列表框。

<Label DockPanel.Dock="Top" > 
      <TextBlock>Selected Pattern</TextBlock> 
     </Label> 
     <TextBox Name="RegexSelectionPattern" IsReadOnly="True" cal:Message.Attach="[Event MouseDoubleClick] = [Action CopySelectedPatternToClipboard()]" DockPanel.Dock="Top" Background="White" Width="222" Height="40" Margin="0,0,4,0" ToolTip="Double click to copy to clipboard"> 
     </TextBox> 
     <Label DockPanel.Dock="Top"> 
      <TextBlock>Dictionary Selection</TextBlock> 
     </Label> 
     <ComboBox x:Name="Dictionaries" SelectedValue="{Binding ActiveRegexLibrary}" IsEditable="False" Margin="0,0,4,0" SelectedValuePath="Value" DisplayMemberPath="Key" DockPanel.Dock="Top"> 
     </ComboBox> 
     <Label DockPanel.Dock="Top"> 
      <TextBlock>Pattern Selection</TextBlock> 
     </Label> 
     <ListBox x:Name="RegexChooser" SelectedItem="{Binding RegexSelection}" Margin="0,0,4,0" SelectedValuePath="Value" DisplayMemberPath="Key" DockPanel.Dock="Top"> 
     </ListBox> 

ShellView 左下方,其中两个是项制表这产生三个控件。

在ShellViewModel中,RegexChooser被绑定为字典,ActiveRegexLibrary供应。问题是

/// <summary> 
    /// List of choosable regex patterns 
    /// </summary> 
    public Dictionary<string, string> RegexChooser 
    { 
     get 
     { 
      return this.ActiveRegexLibrary.dictionary; 
     } 
    } 

当我使用该方法向该字典中添加更多模式时,问题开始发生。 (或者当我尝试向上面的ComboBox添加新的字典时)。当试图向下滚动以查看最新问题时,程序以下面的例外结束。

 Message=Information for developers (use Text Visualizer to read this): 
This exception was thrown because the generator for control 'System.Windows.Controls.ListBox Items.Count:38' with name 'RegexChooser' has received sequence of CollectionChanged events that do not agree with the current state of the Items collection. The following differences were detected: 
    Accumulated count 37 is different from actual count 38. [Accumulated count is (Count at last Reset + #Adds - #Removes since last Reset).] 

One or more of the following sources may have raised the wrong events: 
    System.Windows.Controls.ItemContainerGenerator 
     System.Windows.Controls.ItemCollection 
     MS.Internal.Data.EnumerableCollectionView 
     System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] 
(The starred sources are considered more likely to be the cause of the problem.) 

The most common causes are (a) changing the collection or its Count without raising a corresponding event, and (b) raising an event with an incorrect index or item parameter. 

The exception's stack trace describes how the inconsistencies were detected, not how they occurred. To get a more timely exception, set the attached property 'PresentationTraceSources.TraceLevel' on the generator to value 'High' and rerun the scenario. One way to do this is to run a command similar to the following: 
    System.Diagnostics.PresentationTraceSources.SetTraceLevel(myItemsControl.ItemContainerGenerator, System.Diagnostics.PresentationTraceLevel.High) 
from the Immediate window. This causes the detection logic to run after every CollectionChanged event, so it will slow down the application. 

我注意到这个固定的带助剂的方法是切换ActiveRegexLibrary与虚拟对象,然后将其切换回,以及列表框显示新的图案精。另外,切换到ComboBox中的另一个字典,然后切换回重新加载ListView。 以编程方式刷新这些项目列表的最佳方式是什么? 把

NotifyOfPropertyChange(() => RegexChooser); 
NotifyOfPropertyChange(() => ActiveRegexLibrary); 
作为通常在卡利非列表属性所做的制定者

似乎没有在这里工作,因为我使用的卡利,我想我不应该直接接触与视图VM。

回答

1

我不知道你是否会看到这个答案,但我在这里。

而是使用此:

public Dictionary<string, string> RegexChooser 
{ 
    get 
    { 
     return this.ActiveRegexLibrary.dictionary; 
    } 
} 

尝试使用BindableCollection,像这样:

public BindableCollection<KeyValuePair<String, String>> RegexChooser 
{ 
    get { return this.ActiveRegexLibrary.dictionary; } 
}