0
votes

I'm having problems on Binding a TextBox to a string ( same problem for StringBuilder).

Here is the xaml:

<ListBox x:Name="MyList" ItemsSource="{Binding ListOfBullets, Mode=TwoWay, Converter=StaticResourcedebugConverter}}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                            <local:TaskStepControl Text="{Binding  Mode=TwoWay}" AddHnadler="{Binding DelegateForHandlingAddTaskStep, ElementName=uc}"></local:TaskStepControl>                          
                    </DataTemplate>
                </ListBox.ItemTemplate>

and the Items for the ListBox is:

public ObservableCollection<StringBuilder> ListOfBullets{get{....} set{....}}

I also tried :

public ObservableCollection<string> ListOfBullets{get{....} set{....}}

If I run the app I get an unhalted exception "The parameter is incorrect. " If I remove Mode=TwoWay then it works but as expected editing the Textboxes don't change the text bound object in ListOfBullets;

What am I doing wrong?

1

1 Answers

1
votes

MSDN says that you can't have a two-way binding with an empty property path.

I guess the binding engine can perform a two-way binding only to the specific property, not to the object itself.

My thoughts regarding why it is not allowed:

To keep it simple, you can think that the 'writing' part of the TwoWay binding just sets the provided value on your data source object. So this markup Text={Binding Name, Mode=TwoWay} is similar to that C# code: dataSource.Name = textBox.Text on text updates (of course, the entire binding workflow is much more sophisticated and does other things behind the scenes, but that doesn't matter now).

Your sample tries to do something like listItem = textBox.Text, which doesn't update the real item of your ListOfBullets. Please keep in mind that I have dramatically simplified the way bindings work just to give you an idea of what's happening.

What can you do:

Create a wrapper Model class that will contain your string value and bind to

public ObservableCollection<Model> ListOfBullets {get; set;}

with <local:TaskStepControl Text={Binding Value, Mode=TwoWay} />