2017-03-15 49 views
1

XAMLデザイナデザインビューは、例外が値はnullにはできません。パラメータ名:キー(唯一のXAMLデザイナーのデザインビューで発生)

を投げた

示し例外ArgumentNullException:値をNullにすることはできません。

パラメータ名:キー

のStackTraceが(写真の後を参照)

のInnerException:なし

私はから私を防ぎ、次の問題、上の数日間に苦労されている

すべての影響を受けるビューでXAML Designe rのデザインビューを使用します。

昨日、私は最終的にそれが唯一の設計時に発生し、それがジェネリック型とDataGridののItemsSourceプロパティ(System.Windows間conflitだから、それは難しいparticularyトレースするには、この奇妙な振る舞いを隔離するために管理しましたコントロール)。

だから、これはデザインビュー

System.Collections.Generic.Dictionary 2.FindEntry(TKey key) at System.Collections.Generic.Dictionary 2.TryGetValueでenter image description here

(処理鍵キー、TValue & 値)で上に示されているものです System.Windows.Controls.DataGridItemAttachedStorage.TryGetValue(オブジェクト アイテム、DependencyPropertyプロパティ、オブジェクト&値) System.Windows.Cont でSystem.Windows.Controls.DataGridRow.PrepareRow(対象項目、データグリッド owningDataGrid)で System.Windows.Controls.DataGridRow.SyncPropertiesでrols.DataGridRow.RestoreAttachedItemValue(のDependencyObject objectWithProperty、たDependencyPropertyプロパティ)(ブール forcePrepareCells) System.WindowsでSystem.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(のDependencyObject コンテナ、対象項目)でSystem.Windows.Controls.DataGrid.PrepareContainerForItemOverride(のDependencyObject 要素、対象項目)。プロパティ。 System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(のInt32 childIndexに、UIElementの子、ブールnewlyRealized、ブール isBeforeViewportで System.Windows.Controls.VirtualizingStackPanel.InsertContainer(のInt32 childIndexに、UIElementのコンテナ、ブールisRecycled)でコンテナ) ) でSystem.Windows.Controls.VirtualizingStackPanel。MeasureChild(IItemContainerGenerator & 発電機、IContainItemStorage & itemStorageProvider、 IContainItemStorage & parentItemStorageProvider、& parentItem、 ブール& hasUniformOrAverageContainerSizeBeenSet、ダブル& オブジェクトcomputedUniformOrAverageContainerSize、 &ダブルcomputedUniformOrAverageContainerPixelSize、ブール& computedAreContainersUniformlySized、IListの&項目、オブジェクト&アイテム、 のIList &子供、Int32 & childIndexに、ブール& visualOrderChanged、 ブール& isHorizo​​ntal、サイズ& childConstraint、Rectを&ビューポート、ダブル VirtualizationCacheLength & cacheSizeの、VirtualizationCacheLengthUnit & cacheUnit、ブール& foundFirstItemInViewport、& firstItemInViewportOffset、サイズ& stackPixelSize、サイズ& stackPixelSizeInViewport、サイズ& stackPixelSizeInCacheBeforeViewport、 サイズ& stackPixelSizeInCacheAfterViewport、サイズ& stackLogicalSize、 サイズ& stackLogicalSizeInViewport、サイズ& stackLogicalSizeInCacheBeforeViewport、サイズ& stackLogicalSizeInCacheAfterViewport、ブール& mustDisableVirtualization、ブールisBeforeFirstItem、ブール isAfterFirstItem、ブールisAfterLastItem、ブールskipActualMeasure、 ブールskipGeneration、ブール& hasBringIntoViewContainerBeenMeasured、ブール& hasVirtualizingChildren) at System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(サイズ const raint、NULL可能 previouslyMeasuredOffsets、Nullable`1 & lastPagePixelSize、ブール 再測定) System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(サイズ 制約)で System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(サイズで System.Windows.UIElement.UpdateLayoutでSystem.Windows.ContextLayoutManager.UpdateLayout()でSystem.Windows.UIElement.Measure(サイズ availableSize)(AT System.Windows.FrameworkElement.MeasureCore(サイズ availableSize)で 制約) )

Sample project source code

MyViewModelbase.cs(これは私の一般的なビューモデルベース)

namespace BugProof.ViewModels 
{ 
    using System.Collections.Generic; 

    public class MyViewModelBase<TItem> where TItem : class 
    { 
     public List<TItem> Items { get; set; } 
     public MyViewModelBase() { } 
    } 
} 

MyExtendedViewModel。CS文字列タイプに基づいてされます。この私の拡張ビューモデル、)

namespace BugProof.ViewModels 
{ 
    public class MyExtendedViewModel : MyViewModelBase<string> 
    { 
     public MyExtendedViewModel() 
     { 
     } 
    } 
} 

MainWindow.xaml

<Window x:Class="BugProof.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:vm="clr-namespace:BugProof.ViewModels" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:BugProof" 
     mc:Ignorable="d" 
     d:DataContext="{d:DesignInstance {x:Type vm:MyExtendedViewModel}, IsDesignTimeCreatable=False}"     
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel> 
     <TextBlock>This is what you should se in the designer</TextBlock> 
     <!--Try replacing following DataGrid by a ListBox or ListView--> 
     <DataGrid ItemsSource="{Binding Items}"/> 
    </StackPanel> 
</Window> 

背後MainWindow.xaml.cs(メインウィンドウのコード)

複合体tary詳細:1がリストボックスによってのDataGridコントロールを置き換えた場合の動作は発生しません

  • ListViewコントロールまたはのItemsControl
  • 私はのVisual Studio 2015、バージョン14.0.25431.01アップデートを使用しています3
  • プロジェクトの対象はです.Net Framework 4.5
+1

コレクションに静的でないデフォルト値を使用しようとしましたか? ()=>新しいリスト())? –

+0

戻り値がnullであるかどうかを取得するときにテストする必要があります。 – Safe

+0

こんにちは@ Geert van Horrik。非静的なデフォルト値で試してみると、同じ結果が得られます。しかし、リストをList でMyViewModelBaseクラスに置き換えると、すべてが機能します。それはジェネリック型との衝突です。 –

答えて

4

設定IsDesignTimeCreatable =それはコードが設計時に実行されているかどうかチェックし、ビューモデルのパラメータレスのコンストラクタを実装する特別なケアを必要とするが、トリックをしたMainWindows.xaml真。 IsDesignTimeCreatable = Trueのを設定

According to Microsoft

は、「設計上のインスタンスではなく、デザイナーで生成された代替タイプで、あなたのタイプから作成されることを指定します」。 IsDesignTimeCreatableが設定または に設定されている場合

は驚くべきことに、またaccording to Microsoftは、「すべての設計ツールは、そのバインド可能なプロパティのクラスを解析しているん」。

私は2つの正反対の真理があると思います。これは、文脈に応じて、両方が本当に真である場合もあります。 、ドキュメントの執筆時点で、第2のソースは気づいていなかった、IsDesignTimeCreatableは、それがコレクション(IEnumerableを)見つけるたび値を、デフォルトに設定されると、XAMLデザイナが自動的に生成3つのサンプルかもしれプロパティ

まで証拠そう、これは我々がのListBoxリストビューでのDataGridコントロールを交換する場合は、この質問の問題は発生しないので、のItemsSourceは、IsDesignTimeCreatable = Falseのを汎用コレクションソースにバインドされWPFのDataGrid制御バグがありますまたはItemsControls

関連する問題