wpf 绑定到字符串
我有一个带有 Dim _characters = New ObservableCollection(of String)
的 Movie
类 Characters
是要获取和设置的关联属性,
我如何使用 Binding 使字符显示在列表框中?
到目前为止,我有以下内容,这不起作用,因为我不知道该用什么来代替 ToString
。
<ListBox Name="cList" ItemsSource="{Binding Characters}">
<ItemsControl >
<ItemsControl.ItemTemplate >
<DataTemplate >
<TextBox Text="{Binding ToString}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ListBox>
我希望它们是可编辑的,因此是一个文本框。
我尝试直接将 Characters
绑定到 TextBox
,即使这样也不起作用。
编辑 : 在代码中我有 parentGrid1.DataContext = me.movies
其中
父网格保存电影。
I have a Movie
class with a Dim _characters = New ObservableCollection(of String)
Characters
is the associated property to get and set
How can i get characters to show up in the listBox using Binding?
So far i have the following, this isn't working as i don't know what to put instead of ToString
.
<ListBox Name="cList" ItemsSource="{Binding Characters}">
<ItemsControl >
<ItemsControl.ItemTemplate >
<DataTemplate >
<TextBox Text="{Binding ToString}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ListBox>
I want them to be editable, hence a textbox.
i tried to bind Characters
to TextBox
directly, even that didn't work.
Edit :
in the code i have parentGrid1.DataContext = me.movies
where
parent grid holds movies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于那些遇到异常的人
您可以通过这种方式直接绑定对象:
For those who are experiencing the exception
You can bind the object directly this way:
将您的 TextBox 绑定更改为以下内容。我认为它应该有效:
这会加载项目本身而不是属性或方法输出。由于该项目是一个字符串,因此它应该绑定到字符串值。
Change your TextBox binding to the following. I think it should work:
This loads the item itself instead of a property or method output. Since the item is a string it should bind to the strings value.
您无法对
ObservableCollection
执行双向绑定。为了使字符串可编辑,您必须创建一个具有字符串 get/set 属性的类,如以下类Foo
:您的
Characters
类型应为ObservableCollection
并且您的 XAML 应该更改,以便文本框绑定到Foo.Text
:You cannot perform two-way binding to
ObservableCollection<string>
. In order to make the strings editable you have to create a class with a string get/set property as the following classFoo
:Your
Characters
should then be of typeObservableCollection<Foo>
and your XAML should be changed so that the textboxes are binding toFoo.Text
:只需删除代码的
ToString
部分即可。当前,您正在告诉程序您要绑定到名为
ToString
的对象Just remove the
ToString
portion of the code.Currently you are telling the program that you want to bind to an object called
ToString
我认为角色是公共财产。调试并确保为字符调用 get。如果您有电影页面/窗口的数据上下文,那么您需要将 ListBox 上的 ItemsSource 设置为 {Binding Path=Characters}
I take it that Characters is a public property. Debug and be sure that get is being called for Characters. If you have a the datacontext of the page/window to Movies then you need ItemsSource on the ListBox to be {Binding Path=Characters}