0

I have created a UserControl and then use that control elsewhere but it always throws an exception.

Output:

A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll

A first chance exception of type 'System.TypeInitializationException' occurred in WindowsBase.dll

Call Stack:

PresentationFramework.dll!System.Windows.Markup.XamlReader.RewrapException(System.Exception e, System.Xaml.IXamlLineInfo lineInfo, System.Uri baseUri) + 0x10 bytes

is the topmost call.

It's a basic UserControl with a ListBox inside it and has 3 DP, 2*DataTemplate and a IList for the ListBox's ItemsSource.

Where I use the UserControl I do it like this.

 <CustomUC:MyUserControl ItemsSource="{Binding SomeList}" > <CustomUC:MyUserControl.HeadTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=Name}" /> </StackPanel> </DataTemplate> </CustomUC:MyUserControl.HeadTemplate> </CustomUC:MyUserControl> 

I am not even using one of the templates when trying it out and have tried to comment it out but still no luck.

Even when I'vcommented out all the code that could be throwing exceptions it still won't load.

 <UserControl x:Class="Myproject.CustomUC.MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <ListBox ItemsSource="{Binding Path=Collection}"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <ContentPresenter Name="Head" Visibility="Visible" ContentTemplate="{Binding Path=HeadTemplate}"/> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> </ListBox> </Grid> 

EDIT

Added info. The Visual Designer gives me this error:

Default value type does not match type of property 'HeadTemplate'. 

1 Answer 1

2

I think it could be the

<UserControl x:Class="Myproject.CustomUC:MyUserControl" ... 

That is causing the problem. You've got a : between the CustomUC and the MyUserControl, it should be a .

For more details have a look at the MSDN page for x:Class

Sign up to request clarification or add additional context in comments.

3 Comments

sorry that was a copy/paste error since I changed the name I accidentaly put a : intead of a . but in the original it's the correct .
can you try changing <CustomUC:MyUserControl ItemsSource="{Binding SomeList}" > to <CustomUC:MyUserControl DataContext="{Binding SomeList}" > and then in the control <ListBox ItemsSource="{Binding }">
I found out what it is. I had some UIPropertyMetadata(0) parameter in the DP that came up when I did some shortcut. Didn't notice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.