我正在尝试实现一个承载两个内容区域的 UserControl。一个用于按钮区域,一个用于通用内容区域。但是,当我尝试使用它时,附加的 ButtonArea 没有 Parent 集,因此 RelativeSource Lookup 失败。只有在弹出窗口中使用控件时才会出现此问题,如果在弹出窗口中未使用控件,则一切正常。
<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'
如您所见,查找适用于普通内容,但对于其他按钮区域内容,它会失败。例如,当我使用 Canvas 而不是 Popup 并尝试在 Canvas 上设置一些属性时,它会起作用。当我调试时,我可以看到 VisualTreeHelper.GetParent 为网格返回 null。有人知道为什么在使用 Popup 时会失败吗?