簡體   English   中英

WPF+Caliburn.Micro:ComboBox 和 ListBox 未正確使用字典更新

[英]WPF+Caliburn.Micro: ComboBox and ListBox not updating with dictionary properly

我正在使用 Caliburn.micro 為正則表達式庫制作 GUI,並且在大多數情況下它都有效。 但是當您嘗試加載新字典或正則表達式模式時,將拋出未處理的異常並且程序崩潰。

在我的 shell 視圖 XAML 中,這里是綁定到枚舉鍵值對的字典的 ListBox。

<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>

外殼視圖 這會產生左下角的三個控件,其中兩個是項目列表器。

在 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,然后將其切換回來,並且 ListBox 可以很好地顯示新模式。 此外,切換到 ComboBox 中的另一個字典,然后切換回重新加載 ListView。 以編程方式刷新這些項目列表的最佳方法是什么? 推桿

NotifyOfPropertyChange(() => RegexChooser);
NotifyOfPropertyChange(() => ActiveRegexLibrary);

在 Caliburn 中通常為非列表屬性所做的設置器中似乎在這里不起作用,並且因為我使用的是 Caliburn,所以我認為我不應該直接接觸 VM 的視圖。

我不知道你是否會看到這個答案,但我走了。

而不是使用這個:

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

嘗試使用 BindableCollection,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM