我有问题
System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。 BindingExpression:(无路径);数据项=空;目标元素是“SolidColorBrush”(HashCode=48519443);目标属性是“颜色”(类型“颜色”)
可以使用以下 xaml 重现它:
<Window PreviewKeyDown="Window_PreviewKeyDown"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
... >
<Window.Resources>
<DataTemplate DataType="{x:Type local:ViewModel}">
<ListBox ItemsSource="{Binding List}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Width="20" Height="20">
<Border.Background>
<SolidColorBrush Color="{Binding diag:PresentationTraceSources.TraceLevel=High}" />
</Border.Background>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</Window.Resources>
<ContentControl Content="{Binding}" />
</Window>
and cs:
public class ViewModel
{
public List<Color> List { get; } = new List<Color> { Colors.Red, Colors.Blue, Colors.Green, Colors.Black, Colors.Yellow };
}
public partial class MainWindow : Window
{
readonly ViewModel _vm = new ViewModel();
public MainWindow()
{
InitializeComponent();
}
void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1)
DataContext = _vm;
if (e.Key == Key.F2)
DataContext = null;
}
}
Start, press F1 and check Output
window (binding tracing is enabled, so you will see more).
问题:我该如何修复它们?
错误不会导致功能问题,但它们会出现在Output
窗口(在实际项目中有很多),这已经是一些问题的迹象。哪个问题?
这里的关键是拥有数据模板并应用它一段时间之后,那么datatemplate中的这个绑定就会出现问题。环境DataContext
在构造函数中不会引起问题。
这是一些痕迹,如果有人能够理解它并可以给出一些提示:
System.Windows.Data Warning: 56 : Created BindingExpression (hash=45377043) for Binding (hash=28932383)
System.Windows.Data Warning: 58 : Path: ''
System.Windows.Data Warning: 60 : BindingExpression (hash=45377043): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=45377043): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=45377043): Attach to System.Windows.Media.SolidColorBrush.Color (hash=48519443)
System.Windows.Data Warning: 64 : BindingExpression (hash=45377043): Use Framework mentor <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=45377043): Resolving source
System.Windows.Data Warning: 69 : BindingExpression (hash=45377043): Framework mentor not found
System.Windows.Data Warning: 65 : BindingExpression (hash=45377043): Resolve source deferred
...
System.Windows.Data Warning: 67 : BindingExpression (hash=45377043): Resolving source
System.Windows.Data Warning: 69 : BindingExpression (hash=45377043): Framework mentor not found
System.Windows.Data Warning: 67 : BindingExpression (hash=45377043): Resolving source
System.Windows.Data Warning: 69 : BindingExpression (hash=45377043): Framework mentor not found
System.Windows.Data Warning: 67 : BindingExpression (hash=45377043): Resolving source (last chance)
System.Windows.Data Warning: 69 : BindingExpression (hash=45377043): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:(no path); DataItem=null; target element is 'SolidColorBrush' (HashCode=48519443); target property is 'Color' (type 'Color')
我明白当数据模板创建元素时,mentor由于未找到与源的绑定(导师是什么?),因此绑定创建被推迟。然后它重复几次并因给定错误而失败。但最后它起作用了,也许它继续重复而不追踪它?有任何想法吗?