BindingExpression 路径错误:在“对象"上找不到属性 [英] BindingExpression path error: property not found on 'object'

查看:29
本文介绍了BindingExpression 路径错误:在“对象"上找不到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于出现在输出窗口中的这个错误,我已经搜索了几个小时.我对 WPF 中的绑定很陌生,所以我确定我遗漏了一些东西.

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 错误:39:BindingExpression 路径错误:在对象"字符串"(HashCode=-842352750)上找不到TestItem"属性.BindingExpression:Path=TestItem;DataItem='String' (HashCode=-842352750);目标元素是 'TextBlock' (Name='');目标属性是文本"(类型字符串")

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')

似乎一切正常,但我在输出窗口中收到这些错误.

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>

背后的代码:

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; }

推荐答案

您不想在 UserControl 上设置 DataContext.相反,您希望将其设置在 UserControl 的范围内.

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

通常您在 UserControl 的构造函数中执行此操作.我通常添加这样的一行:

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

this.RootElement.DataContext = myData;

其中 RootElement 是 UserControl(通常是像 Grid 或 StackPanel 这样的面板)的第一个子元素(内容).

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

在你的情况下是:

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

并确保它 InitializeComponent() 调用之后.

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

这只是一个范围界定的问题.您在用户控件的根面板上设置数据上下文.这是 WPF 中一个非常不明显的部分.

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.

更新:正如 Markus 在下面指出的,在列表视图的情况下,您想要设置一个数据数组,而不仅仅是一个数据点.在构造函数中设置 DataContext 时要考虑到这一点.

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.

这篇关于BindingExpression 路径错误:在“对象"上找不到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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