BindingExpression 路径错误:在“对象”上找不到属性;

发布于 2024-10-24 02:40:08 字数 3194 浏览 0 评论 0原文

我已经在输出窗口中出现的这个错误上搜索了几个小时。我对 WPF 中的绑定还很陌生,所以我确信我缺少一些东西。

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

System.Windows.Data 错误:39:BindingExpression 路径错误:在“对象”“字符串”(HashCode=-842352750) 上找不到“TestItem”属性。 BindingExpression:Path=TestItem; DataItem='字符串'(哈希码=-842352750);目标元素是“TextBlock”(名称=“”);目标属性是“文本”(类型“字符串”)

编辑:一切似乎都按预期工作,但我在输出窗口中收到这些错误。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落星ぅ辰 2024-10-31 02:40:08

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

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

this.RootElement.DataContext = myData;

其中 RootElement 是 UserControl(通常是 Grid 或 StackPanel 等面板)的第一个子元素(内容)。

在您的情况下,它将是:

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

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

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

更新:正如马库斯在下面指出的那样,在列表视图的情况下,您需要设置一个数据数组,而不仅仅是一个数据点。在构造函数中设置 DataContext 时请考虑到这一点。

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.

盗琴音 2024-10-31 02:40:08

似乎您将 listviews itemssource 绑定到一个对象,而不是一个数组。一切都在视觉上运作吗?或者你什么也没看到?

编辑
如果你写而不是:

<c:MyData x:Key="myDataSource"/>

this:

<x:Array x:Key="myDataSource" Type="{x:Type c:MyData}">
  <c:MyData />      
</x:Array>

或任何类似的集合定义,会发生什么

seems like you're binding your listviews itemssource to an object, not an array. is everything working visually? or do you see nothing?

EDIT:
what happens if you write instead of:

<c:MyData x:Key="myDataSource"/>

this:

<x:Array x:Key="myDataSource" Type="{x:Type c:MyData}">
  <c:MyData />      
</x:Array>

or any likewise collection definition

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文