Wpf 多个内容,父项为空

Wpf 多个内容,父项为空,wpf,user-controls,parent,relativesource,visual-tree,Wpf,User Controls,Parent,Relativesource,Visual Tree,我试图实现一个承载两个内容区域的UserControl。一个用于按钮区域,另一个用于一般内容区域。但是,当我尝试使用它时,额外的ButtonArea没有父集合,因此RelativeSource查找失败。只有在弹出窗口中使用控件时才会出现此问题,如果在弹出窗口中未使用控件,则一切正常 <Popup x:Name="Popup" PlacementTarget="{Binding ElementName=MyButton}" IsOpen="True" Placement="Right" D

我试图实现一个承载两个内容区域的UserControl。一个用于按钮区域,另一个用于一般内容区域。但是,当我尝试使用它时,额外的ButtonArea没有父集合,因此RelativeSource查找失败。只有在弹出窗口中使用控件时才会出现此问题,如果在弹出窗口中未使用控件,则一切正常

 <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>

这是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>

下面是绑定日志:

    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'
System.Windows.Data警告:54:为绑定(哈希=44842628)创建了BindingExpression(哈希=48564510)
System.Windows.Data警告:56:路径:“”
System.Windows.Data警告:58:BindingExpression(哈希=48564510):默认模式解析为单向
System.Windows.Data警告:59:BindingExpression(哈希=48564510):默认更新触发器已解析为PropertyChanged
System.Windows.Data警告:60:BindingExpression(哈希=48564510):附加到System.Windows.Media.Animation.Storyboard.Target(哈希=39549169)
System.Windows.Data警告:64:BindingExpression(哈希=48564510):RelativeSource(FindAncestor)需要树上下文
System.Windows.Data警告:63:BindingExpression(哈希=48564510):解析源延迟
System.Windows.Data警告:54:为绑定(哈希=4270685)创建了BindingExpression(哈希=23797628)
System.Windows.Data警告:56:路径:“”
System.Windows.Data警告:58:BindingExpression(哈希=23797628):默认模式解析为单向
System.Windows.Data警告:59:BindingExpression(哈希=23797628):默认更新触发器已解析为PropertyChanged
System.Windows.Data警告:60:BindingExpression(哈希=23797628):附加到System.Windows.Media.Animation.Storyboard.Target(哈希=8097004)
System.Windows.Data警告:64:BindingExpression(哈希=23797628):RelativeSource(FindAncestor)需要树上下文
System.Windows.Data警告:63:BindingExpression(哈希=23797628):解析源延迟
System.Windows.Data警告:65:BindingExpression(哈希=48564510):正在解析源
System.Windows.Data警告:68:BindingExpression(哈希=48564510):找到数据上下文元素:(确定)
System.Windows.Data警告:71:Popup类型的查找祖先:查询的BeginStoryboard(哈希=5815700)
System.Windows.Data警告:71:Popup类型的查找祖先:查询的EventTrigger(哈希=13854310)
System.Windows.Data警告:71:Popup类型的查找祖先:查询的CbsuButton(哈希=42847455)
System.Windows.Data警告:71:类型为Popup的查找祖先:查询的网格(哈希=41851983)
System.Windows.Data警告:65:BindingExpression(哈希=23797628):正在解析源
System.Windows.Data警告:68:BindingExpression(哈希=23797628):找到数据上下文元素:(确定)
System.Windows.Data警告:71:类型为Popup的查找祖先:查询的BeginStoryboard(哈希=5019508)
System.Windows.Data警告:71:Popup类型的查找祖先:查询的EventTrigger(哈希=5150477)
System.Windows.Data警告:71:类型为Popup的查找祖先:查询的CbsuButton(哈希=26602688)
System.Windows.Data警告:71:类型为Popup的查找祖先:查询的网格(哈希=44775368)
System.Windows.Data警告:71:Popup类型的查找祖先:查询的CbsuFloatingPanel(哈希=65127114)
System.Windows.Data警告:71:类型为Popup的查找祖先:查询的Popup(哈希=46075905)
System.Windows.Data警告:70:RelativeSource.FindAncestor已找到弹出窗口(哈希=46075905)
System.Windows.Data警告:76:BindingExpression(哈希值=23797628):使用根项目弹出窗口激活(哈希值=46075905)
System.Windows.Data警告:102:BindingExpression(哈希=23797628):使用访问器{DependencyProperty.UnsetValue}将级别0处的项替换为弹出窗口(哈希=46075905)
System.Windows.Data警告:99:BindingExpression(hash=23797628):使用:Popup(hash=46075905)从Popup(hash=46075905)获取级别0的值
System.Windows.Data警告:78:BindingExpression(哈希=23797628):TransferValue-获取原始值弹出窗口(哈希=46075905)
System.Windows.Data警告:87:BindingExpression(哈希=23797628):Trans