解决Set属性'System.Windows.Controls.Decorator.Child'在DataTemplate中引发异常

时间:2013-09-07 04:09:49

标签: wpf

我创建了以下datatemplate:

    <DataTemplate x:Key="StudentDataTemplate" DataType="local:IMDataSet+StudentRow" >
        <DataTemplate.Resources>
            <local:PhoneNumberConverter x:Key="phoneNumConv"/>
            <local:SSIDConverter x:Key="SSIDNum"/>
            <local:GenderConverter x:Key="genderConverter"/>
            <local:DateToAgeConverter x:Key="dateToAgeConverter"/>
            <local:ShamsiConverter x:Key="shamsiConverter"/>
        </DataTemplate.Resources>

        <Grid Name="grdContainer" HorizontalAlignment="Right" MaxWidth="800" MinWidth="250" >
            <Grid.RowDefinitions>
                <RowDefinition Height="45"/>
                <RowDefinition Height="70"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110"/>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <!--Drop Shadow-->
            <Border x:Name="BrdShadow"  BorderBrush="#FFC1C1C1" BorderThickness="1" Margin="0" CornerRadius="4" Grid.ColumnSpan="2" Grid.RowSpan="2">
                <Border.Effect>
                    <DropShadowEffect BlurRadius="18" Direction="0" ShadowDepth="0"/>
                </Border.Effect>
            </Border>

            <Border x:Name="BrdFrame" BorderBrush="#FFC1C1C1" BorderThickness="1" Margin="0" CornerRadius="4" Grid.ColumnSpan="2" Grid.RowSpan="3">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="WhiteSmoke"/>
                        <GradientStop Color="#FFF8F8F8" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
            </Border>

            <Border Margin="10" x:Name="BrdImageFrame" Child="{StaticResource StudentPic}" BorderThickness="0.5" BorderBrush="white" CornerRadius="0,0,2,2" Grid.RowSpan="2" Background="White">
                <Border.Effect>
                    <DropShadowEffect BlurRadius="5" Direction="0" ShadowDepth="0"/>
                </Border.Effect>
            </Border>

            <StackPanel Margin="1" Grid.Column="1" Grid.RowSpan="2">
                <StackPanel Orientation="Horizontal" Margin="0,0,5,0">
                    <TextBlock Name="BlkGender" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Titr" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=Gender, Converter={StaticResource genderConverter}}" VerticalAlignment="Center" Margin="0,0,7,-5"  />
                    <TextBlock x:Name="BlkFullName" TextWrapping="Wrap" FontSize="26.667" FontFamily="/IM;component/Fonts/#Titr" Foreground="{DynamicResource HighImportanceText}" VerticalAlignment="Center"  >
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} {1}">
                                <Binding Path="Name"/>
                                <Binding Path="Family"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                    <TextBlock Name="blkAge" Text="{Binding Path=BornDate, Converter={StaticResource dateToAgeConverter }, StringFormat={} - {0} ساله}" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}"  ToolTip="{Binding Path=BornDate, Converter={StaticResource shamsiConverter}}" VerticalAlignment="Center" TextAlignment="Center" Margin="0,9,0,0"  />
                </StackPanel>

                <TextBlock Name="blkFather" TextWrapping="Wrap" FontSize="20" Foreground="{DynamicResource HalfImportanceText}"  Margin="20,2,0,0" FontFamily="/IM;component/Fonts/#Mitra"   Text="{Binding Path=FatherName, StringFormat=فرزند: {0}}" />
                <TextBlock Name="blkDefContact" TextWrapping="Wrap" FontSize="20" Foreground="{DynamicResource HalfImportanceText}"  Margin="20,2,0,0" FontFamily="/IM;component/Fonts/#Mitra"  Text="{Binding Path=DefaultContact, Converter={StaticResource phoneNumConv}}" FlowDirection="LeftToRight" HorizontalAlignment="Left" />
            </StackPanel>

            <Expander x:Name="expander" IsExpanded="True" Margin="0" VerticalAlignment="Top" d:LayoutOverrides="Width" Grid.Row="2" Grid.ColumnSpan="2" Style="{DynamicResource ExpanderStyle1}" ExpandDirection="Down" >
                <StackPanel Margin="10,10,10,15" x:Name="stkFinName">
                    <TextBlock x:Name="FinName" TextWrapping="Wrap" FontSize="20" FontFamily="Calibri" FontWeight="Bold" Foreground="{DynamicResource HalfImportanceText}"  >
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} {1}">
                                <Binding Path="EnglishName"/>
                                <Binding Path="EnglishFamily"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>

                    <StackPanel Orientation="Horizontal" Margin="8,5,5,0">
                        <TextBlock x:Name="blkBornLoc" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=BornLocation, StringFormat=تولد: {0}}" Margin="0,0,5,0"  />
                        <TextBlock x:Name="blkSSID" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=SSID, Converter={StaticResource SSIDNum}}" Margin="5,0,0,0" FlowDirection="LeftToRight" HorizontalAlignment="Left"   />
                    </StackPanel>
                    <TextBlock x:Name="blkLicense" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding License}" Margin="10,0,10,0"  />
                    <TextBlock x:Name="blkAddress" TextWrapping="Wrap" MaxWidth="250" HorizontalAlignment="Left" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Address}" Margin="10,5,10,0"  />
                </StackPanel>
            </Expander>

        </Grid>
    </DataTemplate>

当我从它创建一个实例时,没有问题,但是当我想创建多个实例(比如在listBox中将它作为itemTemplate使用)时,它会抛出以下异常

设置属性'System.Windows.Controls.Decorator.Child'抛出异常。 InnerException:指定的元素已经是另一个元素的逻辑子元素。首先断开它。

1 个答案:

答案 0 :(得分:1)

问题是由以下行引起的:

<Border ... Child="{StaticResource StudentPic}" ...>

您无法重复使用此类控件。您应该将控件放入模板中。