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>
About these ads

9 Comments »

  1. good solution!!!

    Comment by yossharel — January 20, 2010 @ 10:17 | Reply

  2. Thank you! I spent so much time trying to find a workaround!

    Comment by Viki — October 7, 2010 @ 09:44 | Reply

  3. worked as well for me

    Comment by Kirill Medvedev — December 13, 2010 @ 04:20 | Reply

  4. Sorry, I’ve posted a code, but it was removed in my previous message. TextBox Text=”{Binding .}” worked as well for me.

    Comment by Kirill Medvedev — December 13, 2010 @ 04:26 | Reply

  5. Thanks. This solved my issue too…it was starting to get to me.

    Comment by Jamie — April 27, 2011 @ 03:31 | Reply

  6. Hi,

    I am having same problem with DataGrid and none of the solution is working in my case, I have tried all, with =”{Binding .}” and =”{Binding Path=.}” I am getting the same exception “Two-way binding requires Path or XPath.” and with Binding=”{Binding Path=DataContext, RelativeSource={RelativeSource Self}}” and Binding=”{Binding}}”, data that I type in row just vanishes as soon as focus is lost from that row. Any ideas?

    Thanks,
    Dami.

    Comment by dami — November 9, 2011 @ 15:49 | Reply

    • Code was there in originalpost but didn’t went, here is that again.

      Comment by dami — November 9, 2011 @ 15:53 | Reply

      • Posting code again

        .

        Comment by dami — November 9, 2011 @ 15:58

  7. I have to thank you for the efforts you’ve put in writing this site. I’m hoping to view the same high-grade content from you in the future as well. In truth, your creative writing abilities has motivated me to get my own, personal site now ;)

    Comment by Tomiko Hamontree — March 7, 2012 @ 10:23 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: