WPF: Custom UserControl throwing an Exception
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://开发者_如何学JAVAschemas.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'.
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
精彩评论