86422

Solve Set property 'System.Windows.Controls.Decorator.Child' threw an exception in DataTem

Question:

I have created the following 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>

When i create one instace from it, there is no problem but when i want to create multiple instances (like using it in a listBox as itemTemplate) it throws the following Exception

Set property 'System.Windows.Controls.Decorator.Child' threw an exception. InnerException: Specified element is already the logical child of another element. Disconnect it first.

Answer1:

The problem is caused by the following line:

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

You can't reuse controls like this. You should put the control into the template instead.

Recommend

  • How do I stick Controls inside of ScrollViewer
  • Change old WPF ProgressBar controltemplate to WPF4
  • How to show a grand Total by adding all the prices in the dataGrid column (wpf)
  • BorderThickness doesn't work
  • Custom Brush - I want two gradients chaining them together
  • WPF - No IsEnabled=true behavior using style with animations
  • Listbox SelectionChanged not working with Button in its ItemTemplate
  • WPF Button Image only showing in last control
  • Xcode7 ui testing: staticTexts[“XX”].swipeRight() swipes not far enough
  • Draw smooth line in InkCanvas WPF with Kinect
  • Reading Win32 variables
  • How to change shape of square to dot in XYSplineRenderer Graph
  • JQuery scroll li id to top of div
  • time_t conversion format question
  • new spark.sql.shuffle.partitions value not used after checkpointing
  • Linear gradient not applying in Webkit with d3 generated SVG
  • Converting Twitter DateTime to Localtime with adding UTC offset [duplicate]
  • OpenCL kernel not vectorized
  • Nginx rewrite equivalent to Apache RewriteRule that converts URL params into QueryString key/value p
  • Excel 2007: Format of email address from Outlook 2007
  • How to implement limit with Nhibernate and Sybase
  • HALF_PTR Windows data type
  • why my app based on boost::asio didn't accept new connection
  • div fade-in when window is scrolled a certain distance from the top
  • How to define an array of floats in Shader properties?
  • Unable to connect to OnVif enabled camera using C#
  • A new chart every sheet
  • Setting WPF Window Background to Resource Dictionary Brush User Setting
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • How can I include If-None-Match header in HttpRequestMessage
  • Calculating ratio of reciprocated ties for each node in igraph
  • How to create CGPath from a SKSpriteNode in SWIFT
  • Generate random number from custom distribution
  • How can I speed up CURL tasks?
  • Mysterious problem with floating point in LISP - time axis generation
  • Google Custom Search with transparent background
  • Android fill_parent issue
  • Repeat a vertical line on every page in Report Builder / SSRS
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • How to apply VCL Styles to DLL-based forms in Inno Setup?