我创建了DataTemplate类,如下所示。
namespace WpfApplication2
{
class TemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item != null && item is TaskList)
{
TaskList list = item as TaskList;
Window window = Application.Current.MainWindow;
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(window))
return null;
if (list.Priority == 1)
{
return window.FindResource("defaultTemplate") as DataTemplate;
}
else
{
return window.FindResource("PriTemplate") as DataTemplate;
}
}
return base.SelectTemplate(item, container);
}
}
}
我已经在我的窗口资源中创建了两个datatemplate,如下所示。
<WpfApp2:TaskItem x:Key="taskItem" />
<WpfApp2:TemplateSelector x:Key="tempSelector"></WpfApp2:TemplateSelector>
<DataTemplate x:Key="defaultTemplate">
<Border Name="border" BorderBrush="LightBlue" BorderThickness="1" Padding="5" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Name"></TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Name}" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Item"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Item}" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="Description"></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Description}" />
</Grid>
</Border>
</DataTemplate>
<DataTemplate x:Key="PriTemplate">
<Border BorderBrush="Red" BorderThickness="2" Padding="5" Margin="4">
<DockPanel HorizontalAlignment="Center">
<TextBlock Text="{Binding Description}" Margin="4"></TextBlock>
<Image Margin="4,20,20,20" Source="1.jpg"></Image>
</DockPanel>
</Border>
</DataTemplate>
但是在我加载设计器之后,我得到了以下的删除,我的表单无法加载。
System.Reflection.TargetInvocationException
Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
System.ArgumentNullException
Value cannot be null.
Parameter name: element
当我运行此应用程序时,它工作正常。这只是我在设计模式中看不到的。请指教。感谢。
答案 0 :(得分:3)
我怀疑当你处于设计模式时,Application.Current.MainWindow返回null,所以你的FindResource方法调用是在一个null对象上。
请尝试使用此行:
if ((window == null) || (System.ComponentModel.DesignerProperties.GetIsInDesignMode(window)))
return null;
如果这不起作用,请自行设置以在设计模式下调试组件。说明如下:Walkthrough: Debugging WPF Custom Controls at Design Time