帮助错误“ 'object'不包含'Text'的定义 [英] Help with Error " 'object' does not contain a definition for 'Text' "

查看:1073
本文介绍了帮助错误“ 'object'不包含'Text'的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题:

这是一个使用C#和LINQ to SQL的WPF应用程序。

This is for a WPF app that uses C# and LINQ to SQL.

当用户想要查看客户列表时,他/她开始在文本框中输入名称。 textchanged事件使用输入文本来定义过滤列表的LINQ语句的where子句。

When a user wants to look at a list of customers, he/she begins entering the name in a textbox. The textchanged event uses the input text to define the where clause of a LINQ statement that filters the list.

我目前有两个这样的文本框,基本上运行相同的代码,但我不能将该代码减少到一个单一的功能 - 我将在更多的地方使用客户列表。

I currently have two such text boxes that run essentially the same code, but I cannot reduce that code to a single function - I'll be using customer lists in a bunch more places.

以下是一些代码:

private void CustomerListFiller(object sender, TextChangedEventArgs e)

    {

        string SearchText;

        FrameworkElement feSource = e.Source as FrameworkElement;

        ***SearchText =  sender.Text;*** 

        var fillCustList = from c in dbC.Customers

                           where c.CustomerName.StartsWith(SearchText)

                           orderby c.CustomerName

                           select new

                           {

                               c.CustomerID,

                               c.CustomerName

                           };

粗体,斜体线是问题。我无法弄清楚在StartsWith函数中如何使用发件人的文本值。错误消息是:

The bold, italicized line is the problem. I can't figure out how get at the text value of the sender to use in the StartsWith function. The error message is:

错误1'object'不包含'Text'的定义,没有扩展方法'Text'接受类型的第一个参数可以找到'object'(你是否缺少using指令或程序集引用?)

推荐答案

将sender变量转换为TextBox:

You have to cast the "sender" variable to TextBox:

SearchText =  (sender as TextBox).Text;

这篇关于帮助错误“ 'object'不包含'Text'的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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