wpf 绑定到字符串

发布于 2024-11-27 22:49:18 字数 769 浏览 1 评论 0原文

我有一个带有 Dim _characters = New ObservableCollection(of String)
MovieCharacters 是要获取和设置的关联属性,

我如何使用 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.movi​​es 其中
父网格保存电影。

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 技术交流群。

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

发布评论

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

评论(5

谁许谁一生繁华 2024-12-04 22:49:18

对于那些遇到异常的人

...绑定需要路径或xpath ...

您可以通过这种方式直接绑定对象:

<Label Content="{Binding .}" />

For those who are experiencing the exception

... binding requires path or xpath ...

You can bind the object directly this way:

<Label Content="{Binding .}" />
梦开始←不甜 2024-12-04 22:49:18

将您的 TextBox 绑定更改为以下内容。我认为它应该有效:

<TextBox Text="{Binding}"/>

这会加载项目本身而不是属性或方法输出。由于该项目是一个字符串,因此它应该绑定到字符串值。

Change your TextBox binding to the following. I think it should work:

<TextBox Text="{Binding}"/>

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.

渡你暖光 2024-12-04 22:49:18

您无法对 ObservableCollection 执行双向绑定。为了使字符串可编辑,您必须创建一个具有字符串 get/set 属性的类,如以下类 Foo

public class Foo
{
    string _text;

    public Foo(string text)
    {
        _text = text;
    }
    public string Text 
    {
        get { return _text; }
        set { _text = value; }
    }
}

您的 Characters 类型应为 ObservableCollection 并且您的 XAML 应该更改,以便文本框绑定到 Foo.Text

<ListBox ItemsSource="{Binding Characters}" >
    <ListBox.ItemTemplate >
        <DataTemplate >
            <TextBox Text="{Binding Text}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

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 class Foo:

public class Foo
{
    string _text;

    public Foo(string text)
    {
        _text = text;
    }
    public string Text 
    {
        get { return _text; }
        set { _text = value; }
    }
}

Your Characters should then be of type ObservableCollection<Foo> and your XAML should be changed so that the textboxes are binding to Foo.Text:

<ListBox ItemsSource="{Binding Characters}" >
    <ListBox.ItemTemplate >
        <DataTemplate >
            <TextBox Text="{Binding Text}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
2024-12-04 22:49:18

只需删除代码的 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

墨落成白 2024-12-04 22:49:18

我认为角色是公共财产。调试并确保为字符调用 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}

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