简体   繁体   中英

Xaml designer cannot load after creating the DataTemplate Class

I created DataTemplate class as below.

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);
        }



    }
}

I've already created two datatemplate in my window resource as below.

<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>

But After I load the designer I got the following excpetion and my form cannot load.

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

When I run this application, It's working fine. It's just I cannot see in the design mode. Please advise. thanks.

I suspect Application.Current.MainWindow is returning null when you are in design mode, so your FindResource method call is on a null object.

Try this line instead:

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

If that doesn't work, set yourself up to debug your component in design mode. The instructions are here: Walkthrough: Debugging WPF Custom Controls at Design Time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM