wpf 在wpf中的列表框内绑定文本框列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24017906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:49:57  来源:igfitidea点击:

Bind textbox list inside listbox in wpf

wpfdata-bindingtextboxlistboxdynamic-list

提问by NewInDevelopment

I have to make listbox with textbox in it... and it has to be dynamic. I have observable collection in code behind and I want to bind that for listbox. I want dynamic listbox and this list should have editable textbox in it. So, basically I want to bind multiplr textbox from listbox. Any help would be appreciated

我必须制作带有文本框的列表框......而且它必须是动态的。我在后面的代码中有可观察的集合,我想将它绑定到列表框。我想要动态列表框,这个列表应该有可编辑的文本框。所以,基本上我想从列表框中绑定 multiplr 文本框。任何帮助,将不胜感激

<ListBox HorizontalAlignment="Left" Name="ListTwo" Height="100" Margin="286.769,165.499,0,0" VerticalAlignment="Top" Width="100" ItemsSource="{Binding Source=obs}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Name="TextBoxList"></TextBox>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

By doing this, I have number of textbox same as items in observable collection but textbox's text is not set up.

通过这样做,我的文本框数量与可观察集合中的项目相同,但未设置文本框的文本。

采纳答案by Nitin

You will have to Bind your textbox to the property in your class of which observable collection you have bound

您必须将文本框绑定到您绑定的可观察集合的类中的属性

<ListBox HorizontalAlignment="Left" Name="ListTwo" Height="100" Margin="286.769,165.499,0,0" VerticalAlignment="Top" Width="100" ItemsSource="{Binding Source=obs}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Binding="{Binding PROPERTYINCLASS}"></TextBox>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

回答by Sheridan

If the items in your ObservableCollectionare just plain strings, then you can data bind to the whole string value like this:

如果您的项目ObservableCollection只是普通的strings,那么您可以像这样将数据绑定到整个字符串值:

<ListBox Name="ListTwo" ItemsSource="{Binding Source=obs}" ... >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Name="TextBoxList" Text="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

From the Binding.PathPropertypage on MSDN:

从MSDN 上的Binding.Path属性页:

Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}"is equivalent to Text="{Binding Path=.}".

或者,可以使用句点 (.) 路径绑定到当前源。例如,Text="{Binding}"相当于Text="{Binding Path=.}"

Note that if you had some objects with properties in the collection, then @nit's answer would have been correct as you would need to reference the relevant property name:

请注意,如果您在集合中有一些具有属性的对象,那么@nit 的答案将是正确的,因为您需要引用相关的属性名称:

<ListBox Name="ListTwo" ItemsSource="{Binding Source=obs}" ... >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Name="TextBoxList" Text="{Binding PropertyName}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>