2014-11-14 20 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; } 
}