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 を持つ基本的な UserControl であり、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>

テンプレートを試してみたときにテンプレートの 1 つも使用しておらず、コメントアウトしようとしましたが、まだ運がありません。

例外をスローしている可能性のあるすべてのコードをコメントアウトしても、まだロードされません。

 <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 に答える