WPF / XAML属性的'对象'未找到 [英] WPF/XAML Property not found on 'object'

查看:181
本文介绍了WPF / XAML属性的'对象'未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在一个新的WPF应用程序使用BackgroundWorker的,我需要报告进度/更新,因为它是在后台运行的用户界面。



我一直在做这在Windows中很长一段时间窗体应用程序。 。我刚刚改写这一切WPF和它让我有点头疼的



它保持在运行时引发了以下错误:

$ B $ 40:BindingExpression路径错误:'发件人'
上的'对象'''炭'未找到财产(的hashCode = 5046349 b

System.Windows.Data错误)。
BindingExpression:路径=发件人;的DataItem ='字符'(的hashCode = 5046349);
目标元素是'TextBlock的'(名称=''); target属性是文本
(类型'字符串')System.Windows.Data错误:40:BindingExpression路径
错误:在'对象'''字符$ B $没有找到'主题'属性b(=的hashCode 5046349)。 BindingExpression:路径=主题;的DataItem ='字符
(=的hashCode 5046349);目标元素是'TextBlock的'(名称='');目标
属性为文本(类型'字符串')System.Windows.Data错误:40:
BindingExpression路径错误:'发件人'属性不是'对象'
''字符发现'(的hashCode = 6619237)。 BindingExpression:路径=发件人;
的DataItem ='字符'(的hashCode = 6619237);目标元素是'TextBlock的'
(名称=''); target属性是文本(类型string)
System.Windows.Data错误:40:BindingExpression路径错误:
'主题'属性的'对象'''炭'未找到(=的hashCode 6619237)。
BindingExpression:路径=主题;的DataItem ='字符'(的hashCode = 6619237);
目标元素是'TextBlock的'(名称=''); target属性是文本
(类型'字符串')System.Windows.Data错误:40:BindingExpression路径
错误:在'对象'''字符$ B $没有发现'发件人'属性b(=的hashCode 7536755)。 BindingExpression:路径=发件人;的DataItem ='字符
(=的hashCode 7536755);目标元素是'TextBlock的'(名称='');目标
属性为文本(类型'字符串')System.Windows.Data错误:40:
BindingExpression路径错误:'主题'属性不是'对象'
''字符发现'(的hashCode = 7536755)。 BindingExpression:路径=主题;
的DataItem ='字符'(的hashCode = 7536755);目标元素是'TextBlock的'
(名称=''); target属性是文本(类型string)
System.Windows.Data错误:40:BindingExpression路径错误:'发件人'
上的'对象'未找到物业''字符'(的hashCode = 7536755)。
BindingExpression:路径=发件人;的DataItem ='字符'(的hashCode = 7536755);
目标元素是'TextBlock的'(名称=''); target属性是文本
(类型'字符串')System.Windows.Data错误:40:BindingExpression路径
错误:在'对象'''字符$ B $没有找到'主题'属性b(=的hashCode 7536755)。 BindingExpression:路径=主题;的DataItem ='字符
(=的hashCode 7536755);目标元素是'TextBlock的'(名称='');目标
属性为文本(类型'字符串')System.Windows.Data错误:40:
BindingExpression路径错误:'发件人'属性不是'对象'
''字符发现'(的hashCode = 6357089)。 BindingExpression:路径=发件人;
的DataItem ='字符'(的hashCode = 6357089);目标元素是'TextBlock的'
(名称=''); target属性是文本(类型string)
System.Windows.Data错误:40:BindingExpression路径错误:
'主题'属性的'对象'''炭'未找到(=的hashCode 6357089)。
BindingExpression:路径=主题;的DataItem ='字符'(的hashCode = 6357089);
目标元素是'TextBlock的'(名称=''); target属性是文本
(类型'字符串')System.Windows.Data错误:40:BindingExpression路径
错误:在'对象'''字符$ B $没有发现'发件人'属性b(=的hashCode 6750311)。 BindingExpression:路径=发件人;的DataItem ='字符
(=的hashCode 6750311);目标元素是'TextBlock的'(名称='');目标
属性为文本(类型'字符串')System.Windows.Data错误:40:
BindingExpression路径错误:'主题'属性不是'对象'
''字符发现'(的hashCode = 6750311)。 BindingExpression:路径=主题;
的DataItem ='字符'(的hashCode = 6750311);目标元素是'TextBlock的'
(名称=''); target属性是文本(类型string)
System.Windows.Data错误:40:BindingExpression路径错误:'发件人'
上的'对象'未找到物业''字符'(的hashCode = 6619237)。
BindingExpression:路径=发件人;的DataItem ='字符'(的hashCode = 6619237);
目标元素是'TextBlock的'(名称=''); target属性是文本
(类型'字符串')System.Windows.Data错误:40:BindingExpression路径
错误:在'对象'''字符$ B $没有找到'主题'属性b(=的hashCode 6619237)。 BindingExpression:路径=主题;的DataItem ='字符
(=的hashCode 6619237);目标元素是'TextBlock的'(名称='');目标
属性为文本(类型'字符串')




我不知道这是什么实际意义。一些谷歌搜索没有发现任何帮助。



我还要指出的是,代码没有实际检索所有邮件就好了,如果我不使用在BGWorker在WPF。但它只是停止工作,当我使用的背景工人停止约束力。我不知道为什么。在完全相同的代码工作在的WinForms为BGWorker。



这是什么错误的真正含义是,我能做些什么来摆脱它?



代码隐藏:

 公共部分类主窗口:窗口
{
公共BackgroundWorker的工人=新的BackgroundWorker();
公众的ObservableCollection<消息>消息=新的ObservableCollection<消息>();
MAILMESSAGE公众味精;
诠释计数= 0;

公共类消息
{
公共字符串发送{搞定;组; }
公共字符串主题{搞定;组; }
公共字符串内容{搞定;组; }
公众的DateTime DateReceived {搞定;组; }
公众的DateTime DateRead {搞定;组; }
MAILMESSAGE公共邮件{搞定;组; }
}

公共主窗口()
{
的InitializeComponent();

worker.WorkerSupportsCancellation = TRUE;
worker.WorkerReportsProgress = TRUE;

worker.ProgressChanged + = Worker_ProgressChanged;
worker.RunWorkerCompleted + = Worker_RunWorkerCompleted;
worker.DoWork + = Worker_DoWork;
}

私人无效RetrieveMessages()
{
//工作代码。
使用(VAR IMAP =新AE.Net.Mail.ImapClient())
{
的for(int i = 0; I<计数;我++)
{
味精MAILMESSAGE = imap.GetMessage(我,真,假);
worker.ReportProgress(ⅰ);
}
}
}

私人无效Worker_DoWork(对象发件人,DoWorkEventArgs E)
{
如果(计数!= 0)
RetrieveMessages();
,否则
{
使用(VAR IMAP =新AE.Net.Mail.ImapClient())
{
计数= imap.GetMessageCount(收件箱);
}
}
}

私人无效Worker_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
status.Text =状态:空闲;

list.ItemsSource =消息;
}

私人无效Worker_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
{
Console.WriteLine(msg.Sender.Address ++ msg.Subject);
MessageBox.Show(msg.Subject);
如果(MSG!= NULL)
{
messages.Add(新留言()
{
发件人=喜,
=主题味精.Subject,
含量= msg.Body,
=邮件味精
});

味精= NULL;
}
}

私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
// DEBUG ONLY
worker.RunWorkerAsync() ;
status.Text =状态:同步。
}
}



XAML:

 <的ScrollViewer ScrollViewer.CanContentScroll =真VerticalScrollBarVisibility =自动Horizo​​ntalScrollBarVisibility =已禁用> 
< ListView的X:名称=排行榜的ItemsSource ={绑定源=消息}的Horizo​​ntalAlignment =拉伸VerticalAlignment =拉伸BorderBrush ={X:空}ScrollViewer.Horizo​​ntalScrollBarVisibility =已禁用 >
< ListView.ItemTemplate>
<&DataTemplate的GT;
< StackPanel的方向=垂直的Horizo​​ntalAlignment =拉伸>
< TextBlock的X:名称=senderLabel文本={结合发件人}的Horizo​​ntalAlignment =拉伸TextTrimming =WordEllipsisTextWrapping =包装粗细=SemiBold/>
< TextBlock的X:名称=subjectLabel文本={结合主题}的Horizo​​ntalAlignment =拉伸TextTrimming =WordEllipsisTextWrapping =自动换行/>
< / StackPanel的>
< / DataTemplate中>
< /ListView.ItemTemplate>
< /&的ListView GT;
< /&的ScrollViewer GT;


解决方案

这是一个数据绑定错误



阅读这些最简单的办法就是打破它由冒号/分号,阅读它向后




System.Windows.Data错误:40:BindingExpression路径错误:'发件人'
属性上的'对象'''字符'(的hashCode = 6619237)找不到。
BindingExpression:路径=发件人;的DataItem ='字符'(的hashCode = 6619237);
目标元素是'TextBlock的'(名称=''); target属性是文本
(类型'字符串')





  1. target属性是'文本'(类型'字符串')

  2. 目标元素是'TextBlock的'(名称='');

  3. BindingExpression:路径=发件人;

  4. 的DataItem ='字符'(的hashCode = 6619237);
  5. 不是'对象'''字符'(的hashCode = 6619237)发现
  6. '发件人'属性。

  7. BindingExpression路径错误:

  8. System.Windows.Data错误:40:



1告诉你,有导致错误文本属性



2告诉你的Text属性是在< TextBlock的> 元素



3告诉你绑定快递造成问题的原因为 {绑定路径=发件人}



4告诉你身后的DataItem /的DataContext < TextBlock的> 元素数据类型的项目字符



5告诉你这个实际的问题:有没有属性命名发件人键入字符

$ b的对象
$ b

6只是告诉你这是一个错误的结合



7我的不知道这意味着什么



由于我看到你有一个名为公共财产发件人消息类,它的明确表示,消息不是字符,其明显的,你的的DataContext 为每个项目是错误的。



由于它被设置为一个字符最可能的原因是要绑定到一个字符串,而的DataContext 的每个元素是一个字符字符串。



果然,的ItemsSource ={结合来源=消息} 表示要更改绑定的<从目前的的DataContext code>来源属性设置为字符串。和字符串只是字符数组,所以这意味着你要绑定到字符数组 {M,E,S,S,A,G,E,S}



如果你改变了来源属性设置为路径属性,那么它会正确地读 DataContext.Messages 来代替,并应工作。

 < ListView控件的ItemsSource ={绑定路径=消息}... /> 



(字路径这里是可选的,因为如果你不指定属性名称,然后绑定假定它是路径属性)





作为一个方面说明,我没有看到你设置你的DataContext 表单上的任何地方,我没有看到一个公共的消息财产无论是。



主窗口构造也许应该有一行代码,看起来像这样设置的DataContext 来自己:

  this.DataContext =这一点; 

和你可能需要一个公共财产的的ObservableCollection<消息>消息所以的ListView 绑定可以找到它:

 公开的ObservableCollection<消息>消息
{
{返回消息; }
集合{消息=值; }
}



我不知道,如果这些只被忽视,或者如果你没知道你需要他们。



哦,如果你打算改变这些绑定属性,并具有用户界面自动更新,你要实现的INotifyPropertyChanged~~V 太:)



和,因为我在这里的教程模式,我想我也应该链接到这样的回答:




从Windows窗体过渡到WPF




我会强烈建议通过它阅读(和链接的文章)如果你是新来WPF是如何工作的,并从的WinForms到WPF的切换。这听起来像你:)


I am using a BackgroundWorker in a new WPF app and I need to report progress/update the UI as it is working in the background.

I have been doing this for a long time in WIndows Forms apps. I've just rewritten it all for WPF and it's giving me a bit of a headache.

It keeps throwing the following error at runtime:

System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=5046349)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=5046349); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=5046349)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=5046349); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6357089)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6357089); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=6357089)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=6357089); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6750311)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6750311); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=6750311)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=6750311); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

I have no idea what that actually means. A few Google searches didn't reveal anything that has helped.

I'll also point out that the code does actually retrieve all mails just fine if I don't use the BGWorker in WPF. But it only stops working and stops binding when I use the background worker. I have no idea why. The exact same code works in WinForms for BGWorker.

What does this error really mean and what can I do to get rid of it?

Code-behind:

public partial class MainWindow : Window
    {
        public BackgroundWorker worker = new BackgroundWorker();
        public ObservableCollection<Message> messages = new ObservableCollection<Message>();
        public MailMessage msg;
        int count = 0;

        public class Message
        {
            public string Sender { get; set; }
            public string Subject { get; set; }
            public string Content { get; set; }
            public DateTime DateReceived { get; set; }
            public DateTime DateRead { get; set; }
            public MailMessage Mail { get; set; }
        }

        public MainWindow()
        {
            InitializeComponent();

            worker.WorkerSupportsCancellation = true;
            worker.WorkerReportsProgress = true;

            worker.ProgressChanged += Worker_ProgressChanged;
            worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
            worker.DoWork += Worker_DoWork;
        }

        private void RetrieveMessages()
        {
            // Working code.
            using (var imap = new AE.Net.Mail.ImapClient())
            {
                for(int i = 0; i < count; i++)
                {
                    MailMessage msg = imap.GetMessage(i, true, false);
                    worker.ReportProgress(i);
                }
            }
        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            if(count != 0)
                RetrieveMessages();
            else
            {
                using(var imap = new AE.Net.Mail.ImapClient())
                {
                    count = imap.GetMessageCount("Inbox");
                }
            }
        }

        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            status.Text = "Status: Idle.";

                list.ItemsSource = messages;
        }

        private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Console.WriteLine(msg.Sender.Address + " " + msg.Subject);
            MessageBox.Show(msg.Subject);
            if(msg != null)
            {
                messages.Add(new Message()
                {
                    Sender = "hi",
                    Subject = msg.Subject,
                    Content = msg.Body,
                    Mail = msg
                });

                msg = null;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // DEBUG ONLY
            worker.RunWorkerAsync();
            status.Text = "Status: Synchronizing.";
        }
    }

XAML:

    <ScrollViewer ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
        <ListView x:Name="list" ItemsSource="{Binding Source=Message}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
                        <TextBlock x:Name="senderLabel" Text="{Binding Sender}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis" TextWrapping="Wrap" FontWeight="SemiBold" />
                        <TextBlock x:Name="subjectLabel" Text="{Binding Subject}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis" TextWrapping="Wrap" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ScrollViewer>

解决方案

That is a DataBinding Error

Easiest way to read those is break it up by the colons/semi-colons, and read it backwards

System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

  1. target property is 'Text' (type 'String')
  2. target element is 'TextBlock' (Name='');
  3. BindingExpression:Path=Sender;
  4. DataItem='Char' (HashCode=6619237);
  5. 'Sender' property not found on 'object' ''Char' (HashCode=6619237)'.
  6. BindingExpression path error:
  7. System.Windows.Data Error: 40 :

1 tells you that there is a Text property causing the error

2 tells you that the Text property is on a <TextBlock> element

3 tells you the binding express causing the problem is {Binding Path=Sender}

4 tells you the DataItem/DataContext behind the <TextBlock> element is an item of data type Char

5 tells you the actual problem with this: there is no property named Sender on the object of type Char

6 just tells you it's a binding error

7 I have no idea what it means

Since I see you have a public property named Sender on your Message class, and its clear that Message is not Char, its obvious that your DataContext for each item is wrong.

Since it is set to a Char the most likely cause is you are binding to a string, and the DataContext for each element is a character in that string.

And sure enough, ItemsSource="{Binding Source=Messages} means you are changing the binding's Source property from the current DataContext to a string. And strings are just character arrays, so it means you are binding to the character array { M, e, s, s, a, g, e, s }

If you change the Source property to the Path property, then it will correctly read DataContext.Messages instead, and should work.

<ListView ItemsSource="{Binding Path=Messages}" ... />

(The word Path here is optional, since if you don't specify a property name then the binding assumes it is the value for the Path property)


As a side note, I don't see you setting your DataContext anywhere on the form, and I don't see a public Messages property either.

The MainWindow constructor should probably have a line of code that looks like this to set the DataContext to itself :

this.DataContext = this;

And you probably need a public property for your ObservableCollection<Message> messages so the ListView binding can find it :

public ObservableCollection<Message> Messages
{
    get { return messages; }
    set { messages = value; }
}

I'm not sure if these were merely overlooked, or if you didn't know you needed them.

Oh, and if you plan on changing any of these bound properties and having the UI automatically update, you'll want to implement INotifyPropertyChanged too :)

And since I'm in tutorial-mode here, I figured I should also link to this answer :

Transitioning from Windows Forms to WPF

I would highly recommend reading through it (and the linked articles) if you're new to how WPF works, and are switching from Winforms to WPF. Which it sounds like you are :)

这篇关于WPF / XAML属性的'对象'未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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