【发布时间】:2021-04-24 14:08:40
【问题描述】:
我正在 WPF 中设计一个控件,其中包含一个非常常见的模式:按钮打开下拉菜单。 XAML 的相关部分如下所示:
<ToggleButton x:Name="btnFilterPopup" IsChecked="{Binding IsOpen, ElementName=filterPopup, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}" Margin="{StaticResource DialogItemsExceptLeftMargin}" FontFamily="Marlett" Content="6"/>
<Popup x:Name="filterPopup" PlacementTarget="{Binding ElementName=btnFilterPopup}" Placement="Bottom">
<Border Background="{StaticResource ToolPopupBackgroundBrush}">
<StackPanel Orientation="Vertical" Margin="{StaticResource DialogItemsMargin}">
<CheckBox IsChecked="{Binding FilterCaseSensitive, Mode=TwoWay}" Margin="{StaticResource DialogItemsMargin}">Case sensitive</CheckBox>
<CheckBox IsChecked="{Binding FilterExcludes, Mode=TwoWay}" Margin="{StaticResource DialogItemsExceptTopMargin}">Exclude matching</CheckBox>
</StackPanel>
</Border>
</Popup>
但是,两个绑定都找不到它的目标。诊断如下所示:
System.Windows.Data Warning: 67 : BindingExpression (hash=46479497): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=46479497): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 : Lookup name filterPopup: queried ToggleButton (hash=36168141)
System.Windows.Data Warning: 67 : BindingExpression (hash=46479497): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=46479497): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 : Lookup name filterPopup: queried ToggleButton (hash=36168141)
System.Windows.Data Warning: 67 : BindingExpression (hash=46479497): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=46479497): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 : Lookup name filterPopup: queried ToggleButton (hash=36168141)
System.Windows.Data Warning: 67 : BindingExpression (hash=46479497): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=46479497): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 : Lookup name filterPopup: queried ToggleButton (hash=36168141)
System.Windows.Data Warning: 67 : BindingExpression (hash=46479497): Resolving source (last chance)
System.Windows.Data Warning: 70 : BindingExpression (hash=46479497): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 : Lookup name filterPopup: queried ToggleButton (hash=36168141)
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=filterPopup'. BindingExpression:Path=IsOpen; DataItem=null; target element is 'ToggleButton' (Name='btnFilterPopup'); target property is 'IsChecked' (type 'Nullable`1')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=btnFilterPopup'. BindingExpression:(no path); DataItem=null; target element is 'Popup' (Name='filterPopup'); target property is 'PlacementTarget' (type 'UIElement')
为什么绑定找不到他们的TargetElements?
【问题讨论】:
-
我无法重现该问题,对我而言,使用您的代码似乎一切正常:弹出窗口位于按钮下方,复选框更改属性。您是否为您的窗口设置了
DataContext?请参考minimal reproducible example。 -
也许您正在某处更改
DataContext?在Popup和ToggleButton的父容器中? -
@Sinatr 查看我对 Lennart 答案的评论,为了方便起见,我将在此重复一遍:令人惊讶的是,当我将这两个控件直接剪切并粘贴到主窗口时,它们工作正常。它们被放置在嵌入在 TabControl 中的 UserControl 中(手动,如
)
标签: c# wpf xaml data-binding