CodeCopy

September 10, 2009

Implicit Two Way binding

Filed under: c#, wpf — mazzoo @ 19:12
Tags: , , ,

If you try to use TwoWay binding directly on for example a textbox in a listbox. You will have a problem.

<ListBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ListOfStrings}">
   <ListBox.ItemTemplate>
       <DataTemplate>
          <TextBox Text="{Binding}"/>
       </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

The result you want is obvious, you want to allow the TextBox manipulate the ListOfStrings ( oftype List<string> ).

The error you will get will look something like: Two-way binding requires Path or XPath.

The solution is this, it’s not pretty but it works !

<ListBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ListOfStrings}">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.