将文本框绑定到列表框双向单向源问题?
我使用 textName 让用户输入他的名字。然后键入,textchanged 事件使用与输入匹配的名称更新列表框,然后用户可以单击一个项目(列表框中的 CompletedName),当发生这种情况时,我需要使用项目内容更新文本框。这个问题开始了当我将“GivenName”(作为我查询的表中的一个字段)更改为“CompletedName”时发生。(它是来自查询的字符串连接,如上面所示)
我有这个 LINQ 查询:
var players =
from p in context.Player
where (p.GivenName.StartsWith(TextName.Text.Trim()) || p.Number.StartsWith(TextName.Text) || p.Surname.StartsWith(TextName.Text) )
select new { CompleteName = p.GivenName + " " + p.Surname + " (" + p.Number + ")"};
然后我进行这是名为 listNames 的列表框的源,我有这个文本框:
<TextBox Name="TextName" Text="{Binding ElementName=listNames, Path=SelectedItem.CompleteName}"/>
当我运行它时,显示下一个错误: “Two Way 或 OneWayToSource 绑定无法在类型为 '<>f__AnonymousType0`1[System.String]' 的只读属性 'CompleteName' 上工作”
我理解,当然它不能是 TwoWay 或 OneWayToSource。但我需要用户可以向 textName 添加内容,因为它也是一个搜索文本框,而无需更新列表框中的 SelectedItem。
如果我向文本框中添加表达式 Mode=OneWay.. textName 控件中没有任何反应,我的意思是它不显示列表框中的项目。 我应该做什么才能让它发挥作用?
I use the textName for the user to enter his name. Then typing, the textchanged event updates the listbox with the names that matchs with the input, then the user can click on an item (CompletedName in listbox), and when it happens I need the textbox updates with the item content.. This problem started to happen when I changed the "GivenName" (as a field from the table I query) for the "CompletedName".. (it is a string concat from the query as u see above)
I have this LINQ query:
var players =
from p in context.Player
where (p.GivenName.StartsWith(TextName.Text.Trim()) || p.Number.StartsWith(TextName.Text) || p.Surname.StartsWith(TextName.Text) )
select new { CompleteName = p.GivenName + " " + p.Surname + " (" + p.Number + ")"};
Then I make this the source for a listbox named listNames and I have this textbox:
<TextBox Name="TextName" Text="{Binding ElementName=listNames, Path=SelectedItem.CompleteName}"/>
When I run it, the next error is shown:
"A Two Way or OneWayToSource binding cannot work on the read-only property 'CompleteName' of type '<>f__AnonymousType0`1[System.String]'"
I understand, of course that it can not be a TwoWay or OneWayToSource. But I need the user can add content to the textName, because it is also a search textbox, without updating the SelectedItem on the listbox.
If I add to the textbox the expression Mode=OneWay.. nothing happens in the textName control, I mean it doesnt show the item from the listbox..
What should I do for make it work??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在绑定到匿名类型的实例,但匿名类型的属性是只读的,因此绑定无法更新
CompleteName
属性。无论如何,据我了解,您并不是试图更新名称,文本框实际上是一个搜索框。在这种情况下,您使用的方法将无法工作。您需要处理
TextBox
的TextChanged
事件(或者如果您使用的是 MVVM,则将其绑定到 ViewModel 的属性),并使用新值来选择ListBox
中的适当项目。因此无论如何,您将无法使用匿名类型执行此操作,因为在执行搜索的方法中无法访问其属性......You're binding to an instance of an anonymous type, but properties of anonymous types are read-only, so the binding can't update the
CompleteName
property.Anyway, from what I understand, you're not trying to update the name, the TextBox is actually a search box. In that case the approach you're using cannot work. You need to handle the
TextChanged
event of theTextBox
(or bind it to a property of your ViewModel if you're using MVVM), and use the new value to select the appropriate item in theListBox
. So anyway you won't be able to do this with an anonymous type, since its properties won't be accessible in the method that performs the search...我将为接下来遇到同样问题的人回复我自己的答案。由于我无法使其与 Mode=OneWay 一起工作,所以我这样做了:
这样,而不是使用作为 OnlyRead 源的匿名类型
I will reply my own answer for next people with the same problem. As I couldn't make it work with the Mode=OneWay i did this:
That way, instead of using the Anonimous Type that was the OnlyRead source of