0

I am trying to implement a UserControl that hosts two Content Areas. One for a Button Area and and one general Content area. However when I try to use it the additional ButtonArea does not have a Parent set and therefore RelativeSource Lookup fails. This issue only occurs if the control is used in a popup, if it is not used in a Popup everything works fine.

 <Popup x:Name="Popup" PlacementTarget="{Binding ElementName=MyButton}" IsOpen="True" Placement="Right" DataContext="{Binding PopupContext}">
            <Visuals:CbsuFloatingPanel Title="Set Bining once dependency property" IsBoundToCanvasDimensions="True" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Visuals:CbsuFloatingPanel.ButtonArea>
                    <Grid>
                        <Visuals:CbsuButton Content="Done" Height="25" x:Name="DoneButton">
                            <Visuals:CbsuButton.Triggers>
                                <EventTrigger RoutedEvent="Button.Click">
                                    <BeginStoryboard>
                                        <Storyboard Storyboard.TargetProperty="IsOpen" Storyboard.Target="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Popup}, PresentationTraceSources.TraceLevel=High}">
                                            <BooleanAnimationUsingKeyFrames Duration="0">
                                                <DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
                                            </BooleanAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Visuals:CbsuButton.Triggers>
                        </Visuals:CbsuButton>
                    </Grid>
                </Visuals:CbsuFloatingPanel.ButtonArea>
                <Grid Height="200" Width="200" Background="HotPink">
                    <Visuals:CbsuButton Content="Done" Height="25" x:Name="DoneButton1">
                        <Visuals:CbsuButton.Triggers>
                            <EventTrigger RoutedEvent="Button.Click">
                                <BeginStoryboard>
                                    <Storyboard Storyboard.TargetProperty="IsOpen" Storyboard.Target="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Popup}, PresentationTraceSources.TraceLevel=High}">
                                        <BooleanAnimationUsingKeyFrames Duration="0">
                                            <DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Visuals:CbsuButton.Triggers>
                    </Visuals:CbsuButton>
                </Grid>

            </Visuals:CbsuFloatingPanel>
        </Popup>

And this is the Control template for the UserControl:

 <ControlTemplate TargetType="{x:Type Visuals:CbsuFloatingPanel}">
                <Grid>
                    <Border BorderThickness="0" Margin="0 0 0 -4" CornerRadius="5" Background="{StaticResource Fill2}" 
                      Padding="3">
                        <Border.Effect>
                            <DropShadowEffect BlurRadius="12" Direction="-90" ShadowDepth="1" />
                        </Border.Effect>
                        <DockPanel Width="Auto" HorizontalAlignment="Stretch" Height="{TemplateBinding Height}">
                            <!-- Top Bar With title -->
                            <Border x:Name="TitleBorder"  Height="32" Margin="0" Background="{StaticResource Gradient6}" BorderThickness="0"  DockPanel.Dock="Top"
                                    CornerRadius="5,5,0,0" AutomationProperties.AutomationId="AID_FloatingPanelTitel" >
                                <Grid>
                                    <Visuals:CbsuTextBlock Margin="20,0,20,0" TextWrapping="Wrap" Text="{Binding Title, RelativeSource={RelativeSource TemplatedParent}}" FontSize="{StaticResource FontSizeText}"  FontFamily="{StaticResource FontFamilyBold}" 
                                                            Foreground="{StaticResource FontBrushWhite}" VerticalAlignment="Center" HorizontalAlignment="Stretch"
                                                            AutomationProperties.AutomationId="AID_FloatingPanelTitel_Text"/>
                                    <Thumb Template="{StaticResource MoveThumbTemplate}" x:Name="MoveThumb" Cursor="SizeAll"/>
                                </Grid>
                            </Border>
                            <!-- Bottom Bar with Buttons -->
                            <Border Height="40" Margin="0" BorderThickness="0,0,0,0" BorderBrush="{StaticResource WindowButtonBorder}" CornerRadius="0,0,5,5" 
                                                DockPanel.Dock="Bottom" Background="{StaticResource WindowButtonBackground}">

                                <!-- Right Buttons -->
                                <Grid Width="Auto" HorizontalAlignment="Stretch" AutomationProperties.AutomationId="AID_DLGWND_BUTTONGROUP"
                                        VerticalAlignment="Center" Margin="0 0 10 0">
                                    <ContentPresenter 
                    Name="PART_ButtonAreaContentPresenter"
                    Content="{Binding ButtonArea, RelativeSource={RelativeSource TemplatedParent}}" 
                    DataContext="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}"
                    />

                                </Grid>

                            </Border>
                            <Border x:Name="ContentBorder" Background="{StaticResource ApplicationFill1}" DockPanel.Dock="Top">
                                <!--Message-->
                                <ContentPresenter x:Name="ContentScope"  Margin="0 0 0 0" Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"
                                 AutomationProperties.AutomationId="AID_DLGWND_CONTENT">
                                </ContentPresenter>
                            </Border>
                        </DockPanel>
                    </Border>
                </Grid>
            </ControlTemplate>

And here is the Binding Log:

    System.Windows.Data Warning: 54 : Created BindingExpression (hash=48564510) for Binding (hash=44842628)
System.Windows.Data Warning: 56 :   Path: ''
System.Windows.Data Warning: 58 : BindingExpression (hash=48564510): Default mode resolved to OneWay
System.Windows.Data Warning: 59 : BindingExpression (hash=48564510): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 60 : BindingExpression (hash=48564510): Attach to System.Windows.Media.Animation.Storyboard.Target (hash=39549169)
System.Windows.Data Warning: 64 : BindingExpression (hash=48564510): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 63 : BindingExpression (hash=48564510): Resolve source deferred
System.Windows.Data Warning: 54 : Created BindingExpression (hash=23797628) for Binding (hash=4270685)
System.Windows.Data Warning: 56 :   Path: ''
System.Windows.Data Warning: 58 : BindingExpression (hash=23797628): Default mode resolved to OneWay
System.Windows.Data Warning: 59 : BindingExpression (hash=23797628): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 60 : BindingExpression (hash=23797628): Attach to System.Windows.Media.Animation.Storyboard.Target (hash=8097004)
System.Windows.Data Warning: 64 : BindingExpression (hash=23797628): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 63 : BindingExpression (hash=23797628): Resolve source deferred
System.Windows.Data Warning: 65 : BindingExpression (hash=48564510): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=48564510): Found data context element: <null> (OK)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried BeginStoryboard (hash=5815700)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried EventTrigger (hash=13854310)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried CbsuButton (hash=42847455)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried Grid (hash=41851983)
System.Windows.Data Warning: 65 : BindingExpression (hash=23797628): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=23797628): Found data context element: <null> (OK)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried BeginStoryboard (hash=5019508)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried EventTrigger (hash=51504077)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried CbsuButton (hash=26602688)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried Grid (hash=44775368)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried CbsuFloatingPanel (hash=65127114)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried Popup (hash=46075905)
System.Windows.Data Warning: 70 :   RelativeSource.FindAncestor found Popup (hash=46075905)
System.Windows.Data Warning: 76 : BindingExpression (hash=23797628): Activate with root item Popup (hash=46075905)
System.Windows.Data Warning: 102 : BindingExpression (hash=23797628): Replace item at level 0 with Popup (hash=46075905), using accessor {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 99 : BindingExpression (hash=23797628): GetValue at level 0 from Popup (hash=46075905) using <null>: Popup (hash=46075905)
System.Windows.Data Warning: 78 : BindingExpression (hash=23797628): TransferValue - got raw value Popup (hash=46075905)
System.Windows.Data Warning: 87 : BindingExpression (hash=23797628): TransferValue - using final value Popup (hash=46075905)
System.Windows.Data Warning: 65 : BindingExpression (hash=48564510): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=48564510): Found data context element: <null> (OK)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried BeginStoryboard (hash=5815700)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried EventTrigger (hash=13854310)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried CbsuButton (hash=42847455)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried Grid (hash=41851983)
System.Windows.Data Warning: 65 : BindingExpression (hash=48564510): Resolving source  (last chance)
System.Windows.Data Warning: 68 : BindingExpression (hash=48564510): Found data context element: <null> (OK)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried BeginStoryboard (hash=5815700)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried EventTrigger (hash=13854310)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried CbsuButton (hash=42847455)
System.Windows.Data Warning: 71 :     Lookup ancestor of type Popup:  queried Grid (hash=41851983)
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Primitives.Popup', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'Storyboard' (HashCode=39549169); target property is 'Target' (type 'DependencyObject')
'Hamilton.XRP2.Base.BaseUITestControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll'

As you can see the lookup works for the normal content, but for the additional Button Area content it fails. It works when I use a Canvas for example instead of a Popup and try to set some Property on the Canvas. When I debug I can see that VisualTreeHelper.GetParent returns null for the Grid. Does anybody know why this fails when a Popup is used?

m00ni
  • 113
  • 1
  • 8
  • Didn't you already ask this question today and didn't I already answer it??? – Sheridan Nov 27 '13 at 14:00
  • No you didn't sadly, you fixed the data context problem, for which I am very grateful, but it didn't help with the parent = null issue. So I deleted the question and added this more specific one as this is the more important issue for me. – m00ni Nov 27 '13 at 14:03
  • 1
    [You shouldn't delete a question just because you didn't get an answer](http://stackoverflow.com/help/no-one-answers)... now all the time I spent on your answer which could have helped other users has been wasted. Every time a question is edited, it goes back to the 'top of the pile', so there is no need to delete and start again... if you now have a different problem, then you should have asked a new question, linking to the old one. Either way, it's a bit late now. You'd do better with a new title that correctly reflects your problem... something like 'Popup loses `DataContext`' or similar. – Sheridan Nov 27 '13 at 14:10
  • But thats exactly what you didn't understand in the last question and apparently also not in this one. The Datacontext is there and working, no problems with that. The only thing going wrong is the lookup up the visual tree because whatever I put into ButtonArea has Parent = null. – m00ni Nov 29 '13 at 05:58
  • Well if we didn't understand it last time and we didn't understand it this time, what was the point of adding the same question twice? Also... if nobody can understand your problem, wouldn't it have been better to *edit your original question and explain your problem more clearly*? – Sheridan Nov 29 '13 at 08:52

0 Answers0