Jul*_*bre 4 c# wpf xaml datagrid designer
XAML Designer设计视图显示
抛出异常
ArgumentNullException:值不能为null.
参数名称:key
StackTrace(见照片后)
InnerException:无
在以下问题上我一直在苦苦挣扎几天,这使我无法在每个受影响的视图上使用XAML Designe r的设计视图.
昨天,我最终设法隔离了这个特别难以追踪的奇怪行为,因为它只在设计时发生,而且这似乎是泛型类型和DataGrid的ItemsSource属性(System.Windows.Controls)之间的混淆.
所以,这就是设计视图中显示的内容
在
2.FindEntry(TKey key) at System.Collections.Generic.DictionarySystem.Wolows.Controls.DataGridRow.RestoreAttachedItemValue(DependencyObject)的System.Windows.Controls.DataGridItemAttachedStorage.TryGetValue(Object item,DependencyProperty property,Object&value)的System.Collections.Generic.Dictionary 2.TryGetValue(TKey key,TValue&value)在System.Windows.Coninds.Controls.DataPrid上的System.Windows.Controls.DataGridRow.PrepareRow(Object item,DataGrid owningDataGrid)的System.Windows.Controls.DataGridRow.SyncProperties(Boolean forcePrepareCells)中的objectWithProperty,DependencyProperty属性(DependencyObject元素, System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject)中System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject容器,Object项)的对象项) System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex,UIElement)中的容器)System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex,UIElement child,Boolean newRealized,Boolean isBeforeViewport)处理System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator&generator,IContainItemStorage&itemStorageProvider,IContainItemStorage&parentItemStorageProvider,Object&parentItem)中的container,Boolean isRecycled) ,布尔和hasUniformOrAverageContainerSizeBeenSet,双&computedUniformOrAverageContainerSize,双&computedUniformOrAverageContainerPixelSize,布尔和computedAreContainersUniformlySized,IList中和项目,对象和项目,IList的和孩子,的Int32&childIndex,布尔和visualOrderChanged,布尔和isHorizontal,大小和childConstraint,矩形和视口,VirtualizationCacheLength&CACHESIZE,VirtualizationCacheLengthUnit&cacheUnit,布尔和foundFirstItemInViewport,双&firstItemInViewportOffset,大小和stackPixelSize,Size&stackPixelSizeInViewport,Size&stackPixelSizeInCacheBeforeViewport,Siz E&stackPixelSizeInCacheAfterViewport,大小和stackLogicalSize,大小和stackLogicalSizeInViewport,大小和stackLogicalSizeInCacheBeforeViewport,大小和stackLogicalSizeInCacheAfterViewport,布尔逻辑mustDisableVirtualization,布尔isBeforeFirstItem,布尔isAfterFirstItem,布尔isAfterLastItem,布尔skipActualMeasure,布尔skipGeneration,布尔逻辑hasBringIntoViewContainerBeenMeasured,在System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl布尔&hasVirtualizingChildren)(1& lastPageSafeOffset, ListSystem.Windows上的System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(大小约束)处的System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(大小约束)处的大小约束,Nullable 1和previousMeasuredOffsets,Nullable`1&lastPagePixelSize,Boolean remeasure). System.Windows.Ujelement.UpdateLayo上System.Windows.ContextLayoutManager.UpdateLayout()的System.Windows.UIElement.Measure(Size availableSize)上的FrameworkElement.MeasureCore(Size availableSize)UT()
MyViewModelbase.cs(这是我的通用视图模型库)
namespace BugProof.ViewModels
{
using System.Collections.Generic;
public class MyViewModelBase<TItem> where TItem : class
{
public List<TItem> Items { get; set; }
public MyViewModelBase() { }
}
}
Run Code Online (Sandbox Code Playgroud)
MyExtendedViewModel.cs(这是我的扩展视图模型,它将基于字符串类型)
namespace BugProof.ViewModels
{
public class MyExtendedViewModel : MyViewModelBase<string>
{
public MyExtendedViewModel()
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
MainWindow.xaml.cs(MainWindow的代码背后)
using System.Windows;
namespace BugProof
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
补充细节:
在MainWindows.xaml上设置IsDesignTimeCreatable = True可以解决这个问题,尽管它需要特别注意实现视图模型的参数 - 构造函数,检查代码是否在设计时运行.
根据Microsoft,设置IsDesignTimeCreatable = True,"指定根据您的类型创建设计实例,而不是设计者生成的替代类型".
令人惊讶的是,根据微软的说法,如果IsDesignTimeCreatable未设置或设置为False,"所有设计工具都会解析该类的可绑定属性".
我想我们有两个相反的事实.根据具体情况,甚至可能两者都是真的.可能是,第二个来源在文档编写时并没有意识到,一旦IsDesignTimeCreatable设置为默认的False值,XAML Designer会自动生成3个样本,只要它找到一个集合(IEnumerable)属性
除非另有说明,否则当ItemsSource绑定到泛型集合源并且IsDesignTimeCreatable = False时,这是一个WPF DataGrid控件错误,因为如果我们用ListBox,ListView或ItemsControls替换DataGrid控件,则不会出现此问题.
| 归档时间: |
|
| 查看次数: |
6148 次 |
| 最近记录: |