WPF中的BindingExpression路径错误 [英] BindingExpression path errors in wpf

查看:500
本文介绍了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>

推荐答案

try AncestorType = {x:Type树视图}
try AncestorType={x:Type Treeview}


这里的问题是,因为ItemSource设置为列出它然后成为绑定的当前上下文,因此嵌套在treeView控件内的任何调用属性都必须与列表相关.属性DataContext显然不在List中,这就是为什么找不到它的原因.解决方案是:

将窗口"节点设置为x:Name ="root"或任何您想调用的节点:

The problem here is that, because ItemSource is set to List it then becomes the current context for binding so any property you call that is nested inside the treeView control has to relate to your list. The property DataContext is obviously not in List that is why it cannot be found. The solution would be:

Set Window node to x:Name="root" or whatever you want to call it:

<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路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆