【问题标题】:WPF: Custom UserControl throwing an ExceptionWPF:自定义用户控件引发异常
【发布时间】:2011-06-01 21:09:15
【问题描述】:

我创建了一个 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'.

【问题讨论】:

    标签: wpf xaml user-controls xamlparseexception


    【解决方案1】:

    我觉得应该是

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

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

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

    【讨论】:

    • 抱歉,这是一个复制/粘贴错误,因为我更改了名称,我不小心放了一个 : 而不是 .但在原版中是正确的。
    • 您可以尝试将 更改为 然后在控件中
    • 我发现了它是什么。当我做一些捷径时,我在 DP 中有一些 UIPropertyMetadata(0) 参数。没注意。
    猜你喜欢
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多