0

我创建了一个 UserControl,然后在其他地方使用该控件,但它总是抛出异常。

输出:

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

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

调用堆栈:

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

是最上面的调用。

它是一个基本的用户控件,里面有一个 ListBox,有 3 个 DP、2*DataTemplate 和一个用于 ListBox 的 ItemsSource 的 IList。

在我使用 UserControl 的地方,我会这样做。

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

尝试时我什至没有使用其中一个模板并尝试将其注释掉但仍然没有运气。

即使我注释掉了所有可能引发异常的代码,它仍然不会加载。

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

编辑

添加信息。视觉设计器给了我这个错误:

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

1 回答 1

2

我认为这可能是

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

这就是问题所在。你有一个:在 CustomUC 和 MyUserControl 之间,它应该是一个.

有关更多详细信息,请查看x:Class的 MSDN 页面

于 2011-06-01T13:03:21.560 回答