1

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

<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.

3
  • What is the type of ObservableCollection item? Also what is obs? Is it your collection? If yes then how and where is it defined? Commented Jun 3, 2014 at 14:35
  • ObservableCollection<string> obs = new ObservableCollection<string>(); Its string type.... and its in xaml.cs also binding is like ListTwo.ItemsSource = obs; Commented Jun 3, 2014 at 14:38
  • Then try <TextBox text=Binding"{Binding}" or text=Binding"{Binding path=.}" Commented Jun 3, 2014 at 14:54

2 Answers 2

9

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

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

From the Binding.Path Property page on MSDN:

Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to 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:

<ListBox Name="ListTwo" ItemsSource="{Binding Source=obs}" ... >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Name="TextBoxList" Text="{Binding PropertyName}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Sign up to request clarification or add additional context in comments.

1 Comment

If this answer helped you to solve your problem, then please set it as 'Accepted' as is customary on this website.
1

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>

4 Comments

I have just multiple string in observable collection.
but that would not serve any purpose. if you want editable textbox then you should have some data property backing it so that changes in the data should be reflected to your property....just doing <TextBox Binding="{Binding}"></TextBox> will show you the strings but that wont serve any purpose
what I basically want is like this: I want to allow 5 URLs to store in listbox which is group of textbox. and allow user to edit or remove them. This list should be dynamic..if user have one url it should display one and no empty textbox... I store user prefer urls in setting file of application... So, I load those url from file when application runs
That does not make any difference..you can have class public class UrlDataModel { public string UrlString{get;set;} } and you can create collection of this class and set the UrlString property if url exists in file for UrlDataModel.. then you can bind your text box as <TextBox Binding="{Binding UrlString}"></TextBox>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.