本文介绍了WPF中的BindingExpression路径错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我对WPF和Binding还是很陌生,在输出中出现了一个错误,错误提示:
System.Windows.Data错误:40:BindingExpression路径错误:在``object''``''ImpModel''(HashCode = 38389450)''上找不到``DataContext''属性. BindingExpression:Path = DataContext; DataItem ="ImpModel''(HashCode = 38389450);目标元素是``TreeView''(Name =''_ View''); target属性是"NoTarget"(类型"Object")

这是我的Xaml:

Hey I am pretty new to WPF and Binding I am having an issue in the Output where the error says:
System.Windows.Data Error: 40 : BindingExpression path error: ''DataContext'' property not found on ''object'' ''''ImpModel'' (HashCode=38389450)''. BindingExpression:Path=DataContext; DataItem=''ImpModel'' (HashCode=38389450); target element is ''TreeView'' (Name=''_View''); target property is ''NoTarget'' (type ''Object'')

Here is my Xaml:

<TreeView x:Name="_View" ItemsSource="{Binding List}" Margin="2"

SelectedValuePath="DataContext" ItemTemplate="{StaticResource TreeViewItemTemplate}" >
     <i:Interaction.Triggers>
       <i:EventTrigger EventName="SelectedItemChanged">
       <i:InvokeCommandAction Command="{Binding SelectedTemplateCommand}"

                                    CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}" />
          </i:EventTrigger>
     </i:Interaction.Triggers>
</TreeView>

推荐答案


<Window x:Class="WpfApplication.AllControls.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" x:Name="root">







<TreeView x:Name="_View" ItemsSource="{Binding List}" Margin="2"

SelectedValuePath="{Binding Path=DataContext, ElementName=root}" ItemTemplate="{StaticResource TreeViewItemTemplate}" >
     <i:Interaction.Triggers>
       <i:EventTrigger EventName="SelectedItemChanged">
       <i:InvokeCommandAction Command="{Binding SelectedTemplateCommand}"

                                    CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}" />
          </i:EventTrigger>
     </i:Interaction.Triggers>
</TreeView>



这应该可行.



This should work.


这篇关于WPF中的BindingExpression路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 12:37