2011-03-25 73 views
0

我创建了如下的DataTemplate类。创建DataTemplate后,Xaml设计器无法加载类

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 

当我运行这个应用程序,它工作正常。这只是我在设计模式中看不到的。请指教。谢谢。

回答

3

我怀疑Application.Current.MainWindow在设计模式下返回null,因此您的FindResource方法调用位于空对象上。

试试这个行:

if ((window == null) || (System.ComponentModel.DesignerProperties.GetIsInDesignMode(window))) 
    return null; 

如果不工作,为自己设定了调试在设计模式中您的组件。指令在这里:Walkthrough: Debugging WPF Custom Controls at Design Time

+0

是的,我必须检查如果(窗口== null)返回null;其次是if(System.ComponentModel.DesignerProperties.GetIsInDesignMode(window))谢谢。 – TNA 2011-03-25 07:18:40

相关问题