BindingEx pression路径错误:对财产'对象'未找到 [英] BindingExpression path error: property not found on 'object'

查看:221
本文介绍了BindingEx pression路径错误:对财产'对象'未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个错误出现在输出窗口小时。我是pretty新WPF中绑定的,所以我敢肯定有我丢失的东西。

错误全文(有一个每一个绑定路径,都与此类似):


  

System.Windows.Data错误:39:BindingEx pression路径错误:'TestItem'属性不是'对象'''串'(哈希code = -842352750)找到。 BindingEx pression:路径= TestItem;的DataItem ='字符串'(哈希code = -842352750);目标元素是'TextBlock的'(名称='');目标属性是'文本'(类型'字符串')​​


编辑:似乎一切工作,因为它应该,但我在输出窗口得到这些错误

XAML:

 <&用户控件GT;
    < UserControl.Resources>
        c为C:MyData的X:键=myDataSource/>
        <的DataTemplate X:键=形象>
            <图像X:NAME =TheImage/>
            < D​​ataTemplate.Triggers>
                < D​​ataTrigger绑定={绑定路径=通过失败}VALUE =PASS>
                    <二传手的TargetName =TheImage属性=源VALUE =图像/ accept.png/>
                < / DataTrigger>
                < D​​ataTrigger绑定={绑定路径=通过失败}VALUE =FAIL>
                    <二传手的TargetName =TheImage属性=源VALUE =图像/ delete.png/>
                < / DataTrigger>
                < D​​ataTrigger绑定={绑定路径=通过失败}VALUE =警告>
                    <二传手的TargetName =TheImage属性=源VALUE =图像/ warning.png/>
                < / DataTrigger>
            < /DataTemplate.Triggers>
        < / DataTemplate中>
        <情节提要X:键=OnMouseLeftButtonDown1/>
    < /UserControl.Resources>
    < UserControl.DataContext>
        <绑定源={StaticResource的myDataSource}/>
    < /UserControl.DataContext>
    < ListView的保证金=0,94,-4,-7X:NAME =lsvwOutput的ItemsSource ={绑定源= {StaticResource的myDataSource}}的MouseUp =lsvwOutput_MouseUp的FontFamily =宋体>
        < ListView.View>
            <&GridView的GT;
                < GridViewColumn标题=测试项目WIDTH =300DisplayMemberBinding ={绑定路径= TestItem}/>
                < GridViewColumn标题=信息WIDTH =0DisplayMemberBinding ={绑定路径=信息}/>
                < GridViewColumn标题=结果WIDTH =0DisplayMemberBinding ={绑定路径=通过失败}/>
                < GridViewColumn标题=结果CellTemplate ={StaticResource的形象}/>
            < / GridView的>
        < /ListView.View>
    < /的ListView
< /用户控件>

背后code:

 公共类迈德特:INotifyPropertyChanged的
{
    私人字符串_testitem =;
    私人字符串_information =;
    私人字符串_passfail =;    公共字符串TestItem {
        {返回_testitem; }
        组
        {
            _testitem =价值;
            OnPropertyChanged(TestItem);
        }    }
    公共字符串信息{
        {返回_information; }
        组
        {
            _information =价值;
            OnPropertyChanged(信息);
        }
    }
    公共字符串通过失败{
        {返回_passfail; }
        组
        {
            _passfail =价值;
            OnPropertyChanged(通过失败);
        }
    }
    公共字符串文本{搞定;组; }


解决方案

您不想设置的DataContext的用户控件。相反,你要设置它在该用户的范围。

通常你在该用户的构造函数做到这一点。我通常添加这样一行:

  this.RootElement.DataContext = MYDATA的;

在哪里rootElement的是你的用户控件的第一个子元素(内容)。

(平时喜欢网或StackPanel的面板)

在你的情况将是:

this.lsvwOutput.DataContext = FindResource(myDataSource);

,并确保它的之后的在InitializeComponent()调用。

这只是一个范围的问题。您可以设置用户控件的根面板上的datacontext。这是WPF的一个真正的非显而易见的一部分。

更新:由于下面马库斯指出,在ListView的情况下,你要设置的数据数组,不仅仅是一个数据点。在构造函数设置的DataContext时考虑到这一点。

I've been searching for hours on this error that appears in the output window. I'm pretty new to bindings in WPF, so I'm sure there's something I'm missing.

Full text of the error (there is one for each binding path, all similar to this one):

System.Windows.Data Error: 39 : BindingExpression path error: 'TestItem' property not found on 'object' ''String' (HashCode=-842352750)'. BindingExpression:Path=TestItem; DataItem='String' (HashCode=-842352750); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

EDIT: Everything seems to work as it should, but I get these errors in the output window.

XAML:

<UserControl>
    <UserControl.Resources>
        <c:MyData x:Key="myDataSource"/>
        <DataTemplate x:Key="image">
            <Image x:Name="TheImage" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=PassFail}" Value="PASS">
                    <Setter TargetName="TheImage" Property="Source" Value="Images/accept.png" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=PassFail}" Value="FAIL">
                    <Setter TargetName="TheImage" Property="Source" Value="Images/delete.png" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=PassFail}" Value="WARNING">
                    <Setter TargetName="TheImage" Property="Source" Value="Images/warning.png" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <Storyboard x:Key="OnMouseLeftButtonDown1"/>
    </UserControl.Resources>
    <UserControl.DataContext>
        <Binding Source="{StaticResource myDataSource}"/>
    </UserControl.DataContext>
    <ListView Margin="0,94,-4,-7" x:Name="lsvwOutput" ItemsSource="{Binding Source={StaticResource myDataSource}}"  MouseUp="lsvwOutput_MouseUp" FontFamily="Verdana">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Test Item" Width="300"  DisplayMemberBinding="{Binding Path=TestItem}" />
                <GridViewColumn Header="Information" Width="0" DisplayMemberBinding="{Binding Path=Information}"/>
                <GridViewColumn Header="Result" Width="0" DisplayMemberBinding="{Binding Path=PassFail}"/>
                <GridViewColumn Header="Result" CellTemplate="{StaticResource image}" />
            </GridView>
        </ListView.View>
    </ListView
</UserControl>

Code behind:

public class MyData : INotifyPropertyChanged
{
    private string _testitem = "";
    private string _information = "";
    private string _passfail = "";

    public string TestItem {
        get { return _testitem; }
        set
        {
            _testitem = value;
            OnPropertyChanged("TestItem");
        }

    }
    public string Information {
        get { return _information; }
        set
        {
            _information = value;
            OnPropertyChanged("Information");
        }
    }
    public string PassFail {
        get { return _passfail; }
        set
        {
            _passfail = value;
            OnPropertyChanged("PassFail");
        }
    }
    public string Text { get; set; }

解决方案

You don't want to set the DataContext on the UserControl. Instead, you want to set it in the scope of the UserControl.

Usually you do this in the constructor of the UserControl. I usually add a line like this:

this.RootElement.DataContext = myData;

Where RootElement is the first sub-element (the Content) of your UserControl (usually a panel like Grid or StackPanel).

In your case it would be:

this.lsvwOutput.DataContext = FindResource("myDataSource");

And makes sure that it's after the InitializeComponent() call.

It's just a question of scoping. You set the datacontext on the root panel of the usercontrol. This is a really non-obvious part of WPF.

UPDATE: As Markus points out below, in the case of a listview, you want to set an array of data, not just a data point. Take that into consideration when setting the DataContext in your constructor.

这篇关于BindingEx pression路径错误:对财产'对象'未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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