我在Windows 10中运行了一个UWP项目。在我的mainpage.xml中,我有一个RadDataGrid,它绑定到我的viewmodel上的一个集合,这在运行时正常工作。但是在设计时,我得到一个例外,这使我无法使用设计器。如果我从raddatagrid中删除了ItemsSource,那么设计师就能完美地工作,所以我想这与它有关,但我不确定是什么。
非常感谢
XAML片段:
<Page x:Class="UWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP"
xmlns:common="using:DataObjects"
xmlns:tgrid="using:Telerik.UI.Xaml.Controls.Grid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<tgrid:RadDataGrid x:Name="DataGrid"
ItemsSource="{x:Bind Path=ViewModel.Tasks}"
Grid.Row="1" Grid.Column="0">
</tgrid:RadDataGrid>
模型摘要:
public ObservableRangeCollection<DataObjects.Task> Tasks { get; }
堆栈追踪:
TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at Telerik.Data.Core.BindingExpressionHelper.<>c__DisplayClass0_0.<CreateGetValueFunc>b__0(Object item)
at Telerik.Data.Core.Fields.PropertyInfoFieldInfo.GetValue(Object item)
at Telerik.UI.Xaml.Controls.Grid.DataGridTypedColumn.GetValueForInstance(Object instance)
at Telerik.UI.Xaml.Controls.Grid.Model.GridModel.Telerik.UI.Xaml.Controls.Grid.ITable.GetCellValue(ItemInfo rowItemInfo, ItemInfo columnItemInfo)
at Telerik.UI.Xaml.Controls.Grid.CellsController`1.GetCellDecorator(IItemInfoNode parentRow, ItemInfo columnItemInfo, Int32 rowLine, Int32 columnLine)
at Telerik.UI.Xaml.Controls.Grid.CellsController`1.GenerateCellsForRow(IItemInfoNode rowDecorator, Int32 rowSlot)
at Telerik.UI.Xaml.Controls.Grid.Model.GridModel.GenerateCellsForReadOnlyRow(Int32 rowSlot, Double largestRowElementWidth, IItemInfoNode rowDecorator)
at Telerik.UI.Xaml.Controls.Grid.Model.GridModel.Telerik.UI.Xaml.Controls.Grid.ITable.GenerateCellsForRow(Int32 rowSlot, Double largestRowElementHeight, IItemInfoNode rowDecorator)
at Telerik.UI.Xaml.Controls.Grid.NodePool`2.GenerateCellsForLine(Int32 slot, Double largestLength, T lastElement)
at Telerik.UI.Xaml.Controls.Grid.NodePool`2.GenerateContainer(IList`1 itemInfos)
at Telerik.UI.Xaml.Controls.Grid.NodePool`2.MeasureForward(MeasureContext& context)
at Telerik.UI.Xaml.Controls.Grid.NodePool`2.MeasureVertically(RadSize availableSize, Double offset, Double bufferScale)
at Telerik.UI.Xaml.Controls.Grid.NodePool`2.OnMeasure(RadSize availableSize, Double offset, Int32 frozenElementsCount, Double verticalBufferScale)
at Telerik.UI.Xaml.Controls.Grid.Model.GridModel.MeasureCells(RadSize newAvailableSize)
at Telerik.UI.Xaml.Controls.Grid.RadDataGrid.OnCellsPanelMeasure(RadSize newAvailableSize)
at Telerik.UI.Xaml.Controls.Grid.Primitives.DataGridCellsPanel.MeasureOverride(Size availableSize)
at Windows.UI.Xaml.UIElement.Measure(Size availableSize)
at Telerik.UI.Xaml.Controls.Grid.Primitives.DataGridRootPanel.MeasureOverride(Size availableSize)
at Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)
at Telerik.UI.Xaml.Controls.RadControl.MeasureOverride(Size availableSize)
at Windows.UI.Xaml.UIElement.UpdateLayout()
答案 0 :(得分:0)
如果它可能会破坏您的UI,您可以使用以下内容:
private static bool? _isInDesignMode;
/// <summary>
/// Gets a value indicating whether the control is in design mode (running in Blend
/// or Visual Studio).
/// </summary>
public static bool IsInDesignModeStatic
{
get
{
if (!_isInDesignMode.HasValue)
{
#if SILVERLIGHT
_isInDesignMode = DesignerProperties.IsInDesignTool;
#else
var prop = DesignerProperties.IsInDesignModeProperty;
_isInDesignMode
= (bool)DependencyPropertyDescriptor
.FromProperty(prop, typeof(FrameworkElement))
.Metadata.DefaultValue;
#endif
}
return _isInDesignMode.Value;
}
}
此属性将保护您的代码,并且不会在设计模式下执行。 你也可以像MVVMLight一样使用MVVM库。